Thread: please any body give solution

  1. #1
    kotin
    Join Date
    Oct 2009
    Posts
    132

    Post please any body give solution

    i am new to this forum. In c programing language i got one doubt regarind structures.

    1.how can we define default values to structure members?
    2. i compiled below program by seeing in one forum .its like
    Code:
     struct s
    {
    int i,j;
    s() :i(0),j(0){}
    };
    int main()
    {
    struct s s1;
    printf("%d \n",s1.i);
    printf("%d\n",s1.j);
    return 0;
    }
    but i getting error while compiling the program.

    how can we define our own values to structure by above programing

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by nkrao123@gmail. View Post
    i am new to this forum. In c programing language i got one doubt regarind structures.

    1.how can we define default values to structure members?
    2. i compiled below program by seeing in one forum .its like
    Code:
    #include <stdio.h>
    
     struct s
    {
      int i,j;
    };
    int main()
    {
      struct s s1;
    
      do {
        pintf("Enter the integer value for i: ");
      }while((scanf("%d", &s1.i)) == 0);
    
      printf("Enter the integer value for j: ");
      scanf("%d", &s1.j);
    
      printf("%d \n",s1.i);
      printf("%d\n",s1.j);
      return 0;
    }
    but i getting error while compiling the program.

    how can we define our own values to structure by above programing
    Try that .

  3. #3
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Hi Adak,

    Thanks for your reply.i executed your code. it is ok. i trying to assign some values to struct s at the time of defining(initialising) the structure .i trying to give values to i and j as 0,0 at the time of structure initialisation with the line
    Code:
    s():i(0),j(0){}.

    is it possible to assing some values to variable of structure while initialing the structure?. if possible please explain me how can we do this

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by nkrao123@gmail. View Post
    Hi Adak,

    Thanks for your reply.i executed your code. it is ok. i trying to assign some values to struct s at the time of defining(initialising) the structure .i trying to give values to i and j as 0,0 at the time of structure initialisation with the line
    Code:
    s():i(0),j(0){}.

    is it possible to assing some values to variable of structure while initialing the structure?. if possible please explain me how can we do this

    Edit: Nope, no can do on my compiler. It makes sense that you can't, since at the time you define the struct, there is no instance of the struct yet that has been declared.

    Maybe a different compiler.
    Last edited by Adak; 10-09-2009 at 02:03 AM.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Code:
    #include <stdio.h>
    
    struct structA {
      int a;
      int b;
      int c;
    
      structA() : a(0), b(0), c(0) {}
      structA(const int _a,
              const int _b,
              const int _c) : a(_a), b(_b), c(_c) {}
    };
    
    int main() {
      structA a1;
      printf("%d %d %d\n", a1.a, a1.b, a1.c);
      structA a2(1, 2, 3);
      printf("%d %d %d\n", a2.a, a2.b, a2.c);
    
      return 0;
    }
    by lookin at this i think u will remember class

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Rocky, I've never seen that kind of a struct construction.

    What's the story on that?

    I get tons of errors, mostly:
    Code:
    structA() : a(0), b(0), c(0) {}  //a(0) "Requires constant expression"
    and "Bit fields must contain at least 1..."
    Last edited by Adak; 10-09-2009 at 01:34 AM.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    hey Adak
    itz runnin on my system bro
    and can u tell me whatz new strory here

    Code:
    [rakesh@buildsrv struct]$ cat test.cc
    #include <stdio.h>
    
    struct structA {
      int a;
      int b;
      int c;
    
      structA() : a(0), b(0), c(0) {}
      structA(const int _a,
              const int _b,
              const int _c) : a(_a), b(_b), c(_c) {}
    };
    
    int main() {
      structA a1;
      printf("%d %d %d\n", a1.a, a1.b, a1.c);
      structA a2(1, 2, 3);
      printf("%d %d %d\n", a2.a, a2.b, a2.c);
    
      return 0;
    }
    [rakesh@buildsrv struct]$ g++ test.cc
    [rakesh@buildsrv struct]$ g++ -o test test.cc
    [rakesh@buildsrv struct]$ ./test
    0 0 0
    1 2 3
    [rakesh@buildsrv struct]$
    Check this out from shell

  8. #8
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Hi ada,

    i thinsk

    Code:
    struct s
    {
    int i=10;
    int j=10;
    };
    we cant able decalare struct members . i executed the program by assigning like above. i getting errors.

  9. #9
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Hi Rocky,

    i executed below program

    Code:
    #include<stdio.h>
    struct s
    {
    int i;
    int j;
    s() :i(10),j(10){}
    s(const int _i,const int _j):i(_i),j(j){}
    };
    int main()
    {
    struct s s1;
    printf("%d %d \n",s1.i,s1.j);
    return 0;
    }
    after compiling i getting error as " error: expected specifier-qualifier-list before āsā "

    i look for your further replys

  10. #10
    kotin
    Join Date
    Oct 2009
    Posts
    132
    HI Rocky,

    i am using RHEL5 OS. on wich OS u compiled ? i compiling but i getting error as " error: expected specifier-qualifier-list before āsā"

    can u let me know solution ?

    i look for your further replys

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    He's using gcc under Linux, it seems.

    The devil!

  12. #12
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Fc 10 gcc 4.4.0

  13. #13
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    On Which OS u want i think u must specify that

  14. #14
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Hi Rocks,

    u r right . i compiling on RHEL5 gcc 4.1.1 . in that i find strange problam . while i compiling my program struc.c as #cc struc.c and #gcc struc.c i getting error . but it i compile that same program as #g++ struc.c , its compiling . my stru.c program look likes below

    Code:
    struct s
    {
    int i;
    int j;
    s():i(10),j(10){}
    };
    
    int main()
    {
    struct s s1;
    printf("%d\n",s1.i);
    printf("%d\n",s1.j);
    return 0;
    }
    output : 10
    10


    i didt give the code below also working
    [code]
    s(const int _i,const int _j):i(_i),j(j){}
    [\code]

    i think ,starting i done the mistake as i didt compiled with #g++ struc.c

    can u let me know why my program is compiling with g++ onlly , why it didt compiling with cc and gcc and giving error as " error: expected specifier-qualifier-list before āsā "

    and why we cant able to initialise the variables in structures like

    struct s
    {
    int i=10;
    int j=20;
    };


    i look for your further replys

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    nkrao123@gmail., is this a C or C++ program that you are trying to write? You appear to be trying to use a C++ constructor initialisation list, but you posted in the C programming forum and use I/O functions that are from the C standard library (although they are also available in the C++ standard library).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-29-2004, 05:23 PM
  2. Why dont 56k modems give 56kbps?
    By MovingFulcrum in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 04-24-2002, 08:15 AM
  3. Radiohead rule
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-18-2002, 07:37 PM
  4. How To Give A Font Colour ?
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 09-14-2001, 01:22 PM