I need help

This is a discussion on I need help within the C Programming forums, part of the General Programming Boards category; Hi People, I am trying to understand of how pointer to struct works. And I am running into a problem. ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    13

    I need help

    Hi People,

    I am trying to understand of how pointer to struct works. And I am running into a problem.

    Not sure why the last line "printf("Struct pointer with Name: %s\n",e2[0]->Name);" is showing :-

    Struct pointer with Name: This is a second struct.

    I thought that it would show:-

    Struct pointer with Name: This is the first struct.

    However, it does not. Would any one pls help me out.


    Thanks,

    zbonzbon


    Code:
    **********************************************************
     * Array.c - Learning how array works.
     **********************************************************/
    
    
    #include <stdio.h>
    #include <string.h>
    
    
    int main(){
    
    	struct example{
    		char Name[40];
    	};
    
    	char *pt_Name[2]= { 
    		"This is the first struct",
    		"This is a second struct"
    	};
    
    	struct example *e2[2];
    	int i;
    
    	for(i=0; i<2; i++){
    
    		struct example e;
    		strcpy(e.Name,pt_Name[i]);
    		e2[i]=&e;
    
    
    	}
    
    	printf(" %s\n",e2[0]->Name);
    
    	return 0;
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Array.c - Learning how array works.
    You know about structures before arrays?

    That code shouldn't work properly. You're taking the address of a local variable.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
     struct example *e2[2];
    	int i;
    
    	for(i=0; i<2; i++){
    
    		struct example e;
    		strcpy(e.Name,pt_Name[i]);
    		e2[i]=&e;
    
    
    	}
    
    	printf(" %s\n",e2[0]->Name);
    See? e2[]'s elements point to a variable declared in the for loop, and then you try to print it after the for loop, when e no longer exists!

    Get rid of the pointers; make *e2 into e2, and try it that way.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    Hi dwks,

    1. I was just finishing to learn how array in C works and then I just use the same file to add the structure to it. That is why you see - Array.c - Learn how array works.

    2. What do you mean by local variable ? Are you refer to *e2 ? Would you pls give me a little more explaining.

    Thanks,

    zbonzbon

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    e is a variable local to the for loop. It's scope ends when the for loop ends. But you take it's address, assign it to a variable, and try to access it after the for loop! e no longer exists.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    Hi dwks,

    Ah. I got it. Thanks

    Regards,

    zbonzbon

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    So, did you fix it? Here's a program that uses an array:
    Code:
    #include <stdio.h>
    
    int main(void) {
        char *s[] = {"false", "true"};
        int x;
    
        printf("\nEnter a number: ");
        scanf("%i", &x);
        if(!x) puts(s[0]);
        else puts(s[1]);
    
        return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    Hi dwks,

    1. Yes, I just change *e2 into a normal an array of structs - struct example e2[2]. Then it would work nicely. However, one important point I learn today (actually from you) is that - Do not use pointer to point to a local variable.

    2. Thanks for the program I will try it.

    Thanks,

    zbonzbon

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    Hi dwks,

    BTW, I sometimes saw a code and it starts liked this:

    int main(argc, char argv[])

    I kind of understand that - argc is an argument count on the command line.

    argv[] is where it stores the commands ?....

    Another the words is that I am not quite 100% understand it.

    Would you help ?

    Thanks,

    zbonzbon

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Actually, you probably meant
    Code:
    int main(argc, char *argv[])
    I kind of understand that - argc is an argument count on the command line.

    argv[] is where it stores the commands ?....
    argv[0] is usually the program's name, that is, the path to the executable. The rest of argv[] is the arguments passed on the command line. argc is the number of items in argv[].
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[]) {  /* or **argv */
        int x;
        printf("argc: %i\n", argc);
    
        for(x = 0; x < argc; x ++) {
            printf("argv[%i]: %s\n", x, argv[x]);
        }
    
        return 0;
    }
    Code:
    C:\DWK>testargs
    argc: 1
    argv[0]: C:\DWK\TESTARGS.EXE
    
    C:\DWK>testargs hippo testing
    argc: 3
    argv[0]: C:\DWK\TESTARGS.EXE
    argv[1]: hippo
    argv[2]: testing
    
    C:\DWK>testargs "This is a string with spaces in it" hippo
    argc: 3
    argv[0]: C:\DWK\TESTARGS.EXE
    argv[1]: This is a string with spaces in it
    argv[2]: hippo
    
    C:\DWK>
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    Hi dwks,

    1. Thanks a lot for your help.

    2. Another hard topic that I also did not understand is - Link list. I have been trying to read and read. But most are very hard understand. Can you give me a basic simple program with link list and explain what they are?

    Thanks again,

    zbonzbon

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Well, I could. But read the FAQ first. Then I can, if you want.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    Hi dwks,

    Thanks, I will read it now and I will post my questions later.

    Regards,

    zbonzbon

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21