Struct integer member has wrong value

This is a discussion on Struct integer member has wrong value within the C Programming forums, part of the General Programming Boards category; For some reason whenever I enter an integer value to the integer type member of the struct, and then printing ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    2

    Struct integer member has wrong value

    For some reason whenever I enter an integer value to the integer type member of the struct, and then printing them, I get insanely high values. What am I doing wrong? I absolutely have no idea, please help D:

    EDIT: printing the struct without '&' characters in front of the variable name makes the program crash (so the current code crashes for me), but when I add them, the integer members have completely bad values, however the character ones are fine.

    Code:
    #include <stdio.h>
    
     struct rec1 {
            char* Code;
            char* Name;
            int StudentMax;
            int StudentIn;
       } Subject[2];
    
    
        int main()
        {
            int i;
            int j;
    
            for(i = 0; i <= 1; i++)
            {
                printf("Subject[%d].Code = ",i);
                scanf("%s", &Subject[i].Code);
                printf("Subject[%d].Name = ",i);
                scanf("%s", &Subject[i].Name);
                printf("Subject[%d].StudentMax = ",i);
                scanf("%d", &Subject[i].StudentMax);
                printf("Subject[%d].StudentIn = ",i);
                scanf("%d", &Subject[i].StudentIn);
            }
    
    
                for(j = 0; j <= 1; j++)
                {
                printf("Subject[%d].Code = %s \n",j,Subject[j].Code);
                printf("Subject[%d].Name = %s \n",j,Subject[j].Name);
                printf("Subject[%d].StudentMax = %d \n",j,Subject[j].StudentMax);
                printf("Subject[%d].StudentIn = %d \n",j,Subject[j].StudentIn);
                }
    
            return 0;
        }
    Last edited by DoomerMrT; 02-23-2013 at 06:51 AM.

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Because your char* Code and Name variables aren't suitable for reading strings into. A char* can point to a string, but in this case they don't point to anything. You need to allocate some space for the strings you're going to read in. You could do this dynamically with malloc, or statically by making Code and Name arrays (but they'll be of a fixed length).

    The reason your ints get ridiculous values is because the scanf will read the string into memory - the ints will be next to the char*s in memory so they'll get corrupted.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    2
    Thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct as a Union member
    By csharp100 in forum C++ Programming
    Replies: 6
    Last Post: 07-08-2012, 08:38 PM
  2. Indicate the member of the struct
    By Hannibal2010 in forum C Programming
    Replies: 15
    Last Post: 12-10-2011, 08:51 AM
  3. Assign struct member value to struct member value
    By thahemp in forum C Programming
    Replies: 5
    Last Post: 10-13-2010, 09:48 AM
  4. printf of struct member
    By dassybr in forum C Programming
    Replies: 3
    Last Post: 11-21-2009, 02:22 PM
  5. Replies: 1
    Last Post: 05-05-2004, 06:58 AM

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