Like Tree1Likes
  • 1 Post By grumpy

help with pointers and structs in C

This is a discussion on help with pointers and structs in C within the C Programming forums, part of the General Programming Boards category; in this code Code: #include <stdio.h> struct hospital { int num_beds; char code; char *address; }; int main() { struct ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    29

    help with pointers and structs in C

    in this code
    Code:
    #include <stdio.h>
    
    struct hospital {
    	int num_beds;
    	char code;
    	char *address;
    };
    
    int main() {
    	struct hospital h1, h2;
    	struct hospital *main;
    	main = &h1;
    	h1.num_beds=23;
    	printf("h1.num_beds: %d\n",h1.num_beds);
    	h2 = h1;
    	printf("h2.num_beds: %d\n",h2.num_beds);
    	h1.address = "123 Main St.";
    	main->address = "45 Elmo Drive";
    	printf("h1.address: %s\n", h1.address);
    	printf("h2.address: %s\n", h2.address);
    	printf("main->address: %s\n", main->address);
            return 0;
    }
    the output is
    Code:
    h1.num_beds: 23
    h2.num_beds: 23 )
    t$1EDEss: 
    h2.address:
    D$$9u
    [^_]$USt1u[]US
    main->address: 45 Elmo Drive

    why is this line printing gibberish ?
    printf("h1.address: %\n", h1.address);
    Last edited by omega666; 04-17-2011 at 07:32 PM.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    589
    I think it might have to do with the assignment statement h2=h1

  3. #3
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Your problem is char *address. This is a pointer to a single character, but you are trying to store many characters there. Try something like
    Code:
    struct hospital {
    	int num_beds;
    	char code;
    	char address[128];
    };

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    29
    how can that be a problem, since this is valid
    char *test="hello";

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    5,871
    The format specifier you want is "%s\n".
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    That's because that is part of a declaration; the compiler will automatically allocate enough space for the whole string. But if you try something like

    Code:
    char *test;
    /*do some stuff*/
    test = "Hello";
    then the compiler might only allocate enough space for one character, because that's all you've asked it for. If other variables happen to get stored right next door, and you try to store a longer string in test, then you will overwrite the other variables.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    29
    if the h1.address is currupt because of fitting more then 1 chars into a 1 char space
    then why does the last print statement print properly
    main->address: 45 Elmo Drive
    isnt main->address the same memory as h1.address ?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by TheBigH View Post
    That's because that is part of a declaration; the compiler will automatically allocate enough space for the whole string. But if you try something like

    Code:
    char *test;
    /*do some stuff*/
    test = "Hello";
    then the compiler might only allocate enough space for one character, because that's all you've asked it for. If other variables happen to get stored right next door, and you try to store a longer string in test, then you will overwrite the other variables.
    Actually in the example you gave the compiler will allocate no memory at all. Al you've created is a pointer.

    For our friend's idea to work he needs...
    Code:
    struct hospital {
    	int num_beds;
    	char code;
    	char address[64];
    };
    
    struct hospital main;
    
    strcpy (main.address,"123 Any street");
    This is for two reasons... first he needs to create space *in the struct* to hold his string and in C you cannot assign strings across the equal's sign.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    29
    i still dont understand this
    isnt main->address the same memory as h1.address ?

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by omega666 View Post
    i still dont understand this
    isnt main->address the same memory as h1.address ?
    Yes it is... and in neither case did you create any space for your string.

    A string such as "Hello" requires 6 characters... char h[6]; ... that's 5 for the text and 1 for the trailing null. You cannot assign stuff to pointers without first giving the pointer something to point to... strings take space...

    Try it as I suggested... does it work?

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    29
    if there were no space for the string, then how the the last printf statement print properly using the variable 'main->address' ?
    whats the difference between printing with
    h1.address and main->address, if the address variable was defined as a pointer to a char which only has room for 1 char?

    i changed the char statement, and i get

    new.c:17: error: incompatible types in assignment
    new.c:18: error: incompatible types in assignment

    which are

    h1.address = "123 Main St.";
    main->address = "45 Elmo Drive";
    Last edited by omega666; 04-17-2011 at 09:20 PM.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by omega666 View Post
    if there were no space for the string, then how the the last printf statement print properly using the variable 'main->address' ?
    whats the difference between printing with
    h1.address and main->address, if the address variable was defined as a pointer to a char which only has room for 1 char?
    Mostly, just dumb luck... it's pointing to a string literal which is stored as part of the data segment in your program, so it can know an address for it. There is space for your string... but it's not where or what you expect. Try doing a strcpy() or strcat() to that pointer and see what happens.

    C is a language with NO STRINGS... what we call "strings" are actually arrays of characters. There are a number of library functions that will manipulate arrays of characters as though they are strings... but these are not real strings like you would find in Pascal or D... they're fake strings created as chains of individual characters terminated by a 0. What this means is that you simply cannot do string3 = string1 + string2 C simply does not know how to do that.

    Here try this simple experiment if you don't believe me...
    Code:
    char *string1;
    char string2[] = "Hello";    //--- looks good so far, doesn't it?
    char string3[] = " world!"  //--- this is coole too, right...
    
    string1 = string2 + string3;
    
    printf("%s", string1);  // ---- ummm, maybe not!
    There are some things you can get away with when playing with string literals... but when you get into manipulating character arrays, the rules are very different.
    Last edited by CommonTater; 04-17-2011 at 09:29 PM.

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    5,871
    Quote Originally Posted by TheBigH View Post
    That's because that is part of a declaration; the compiler will automatically allocate enough space for the whole string. But if you try something like

    Code:
    char *test;
    /*do some stuff*/
    test = "Hello";
    then the compiler might only allocate enough space for one character, because that's all you've asked it for. If other variables happen to get stored right next door, and you try to store a longer string in test, then you will overwrite the other variables.
    Rubbish.

    The assignment test = "Hello" is valid, and makes test point to the first character in the string "Hello" (i.e. at the 'H').

    There is a problem if code subsequently attempts to do *test = 'B'; (to change the 'H' into a 'B') as that modifies a string literal. Since string literals are const, that is undefined behaviour.

    Note: it is actually a historical anomaly in the C language that it is possible to assign a pointer to char (which is non-const) so it points at contents of a string literal.
    claudiu likes this.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. int pointers in structs
    By davo666 in forum C Programming
    Replies: 7
    Last Post: 01-23-2009, 11:17 PM
  2. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 06:14 PM
  3. need help with structs and pointers
    By rozner in forum C Programming
    Replies: 4
    Last Post: 04-07-2003, 03:33 PM
  4. Help with pointers and structs
    By NewToC in forum C Programming
    Replies: 8
    Last Post: 12-13-2002, 06:04 AM
  5. Pointers to structs
    By csmatheng in forum C Programming
    Replies: 2
    Last Post: 09-11-2002, 03:47 AM

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