Thread: Assignment to a structure array member

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    10

    Assignment to a structure array member

    Hi

    im having problems with assigning to a value directly to a member of an array structure.

    my structure defination is:

    Code:
    struct student {
    	int ID;
    	char Name[50];
    	char Date[10];
    	float Min_temp;
    	float Max_temp;
            int Valid;
    } record[20];
    I have already initialised these values successfully for my 20 records, however in a later bit of code im trying to change the value of 'Valid' for a particular record from 1 to 0.

    I am trying to do this for any given record of the array using a variable, the value of which is botained from a scanf. The whole program compiles successfully, the problem is purely that the value is not changed from 1 to 0 when i wish it to be.

    my assignment line looks like this :

    record[x].Valid = 0;

    where x is the value obtained from the scanf.

    any help with this would be hugely appreciated, as it is the only thing not working correctly right now! :P

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I suspect it's something else that is wrong, since the one line of assignment you show looks perfectly fine to me, assuming of course x is within 0..19.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Beginner leiming's Avatar
    Join Date
    Jan 2008
    Location
    Fujian, China
    Posts
    25
    this is the code I tested:
    Code:
    #include <stdio.h>
    
    struct student {
    	int ID;
    	char Name[50];
    	char Date[10];
    	float Min_temp;
    	float Max_temp;
        int Valid;
    } record[20];
    
    int main(){
    	int i, x;
    	for(i = 0; i < 20; i++)
    		record[i].Valid = 1;
    	scanf("&#37;d", &x);
    	record[x].Valid = 0;
    	for(i = 0; i < 20; i++)
    		printf("%2d: %d\t", i, record[i].Valid);
    	return(0);
    }
    I named it "1.cpp" and then compiled it.
    this is the result:
    Code:
    1.exe
    3
     0: 1    1: 1    2: 1    3: 0    4: 1    5: 1    6: 1    7: 1    8: 1    9: 1
    10: 1   11: 1   12: 1   13: 1   14: 1   15: 1   16: 1   17: 1   18: 1   19: 1
    
    
    1.exe
    5
     0: 1    1: 1    2: 1    3: 1    4: 1    5: 0    6: 1    7: 1    8: 1    9: 1
    10: 1   11: 1   12: 1   13: 1   14: 1   15: 1   16: 1   17: 1   18: 1   19: 1
    
    
    1.exe
    9
     0: 1    1: 1    2: 1    3: 1    4: 1    5: 1    6: 1    7: 1    8: 1    9: 0
    10: 1   11: 1   12: 1   13: 1   14: 1   15: 1   16: 1   17: 1   18: 1   19: 1
    It seems nothing wrong.

    You may have a try to add
    printf("%d\n", x);
    before
    record[x].Valid = 0;
    to see if "x" has strange value.

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    You'll need to range check x to make sure it's between 0 and 19. Can you post the code that you have? It'll be easier to see what's wrong then.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    10
    thanks alot guys i managed to solve my problem with your help, it was to do with how i had nested the assignment within several If statements - quite messy :P

    sorry i know its not helpful not posting my entire code its just im reluctant to as it is for an assignment

    thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. better way to scanf of a array of structs member?
    By stabu in forum C Programming
    Replies: 3
    Last Post: 07-17-2008, 04:51 PM
  2. How to sort an array of pointers to structure
    By broli86 in forum C Programming
    Replies: 3
    Last Post: 06-30-2008, 02:52 PM
  3. Replies: 2
    Last Post: 04-19-2008, 12:06 AM
  4. Dynamic structure with array and array count
    By Nazgulled in forum C Programming
    Replies: 14
    Last Post: 06-08-2007, 10:10 PM
  5. array in structure help
    By bobnet in forum C++ Programming
    Replies: 4
    Last Post: 10-25-2003, 07:51 AM