Thread: Save Data from the global variable into the linked list

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    25

    Save Data from the global variable into the linked list

    Hi guys

    I'm trying to run so called student-administration program.

    I got functions that can read and write student data, but the one saving the data from about 30 students has some problem that I can't figure. (warning: I'm quite new to C programming)

    so this is the code:..I guess I can't use global variables as function arguments?

    Code:
    //global variables
    static char ReturnName[31];
    static char ReturnFirstName[31];
    static float ReturnAverage;
    
    
    
    typedef struct{
    
    	char Name[31];
    	char FirstName[31];
    float Average;
    
    }Student;
    
    //defines the linked list for 30 Students
    Student Students[30];
    
    
    //program to save all 30s
    void SaveData(ReturnName,ReturnFirstName, ReturnAverage){
    
     int i;
    	for(i=1;i<=30;i++){
    
    		//check if the students is already registered
    		if(!((Students.Name)&&(Students.FirstName))){
    
    		Students[i].Name=ReturnName;
    		Students[i].FirstName=ReturnFirstName;
    	}
    	}
    }
    the errors:
    Code:
    incompatible types when assigning to type 'char[31]' from type 'int'

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > for(i=1;i<=30;i++)
    Arrays begin at 0, not 1

    > Students[i].Name=ReturnName;
    You need to use strcpy to copy one char array to another char array.

    > void SaveData(ReturnName,ReturnFirstName, ReturnAverage)
    You should prototype your function to expect char arrays.
    It seems to implicitly believe that these parameters are integers.

    > I guess I can't use global variables as function arguments?
    Of course you can, you just need to fix a bunch of other errors to begin with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Salem View Post
    > for(i=1;i<=30;i++)
    Arrays begin at 0, not 1
    I bet that the confusion comes from the <= in the control part of the for loop. Well, yes, if you start from 1 and count until 30(included), you are going to do 30 loops.
    But as already Salem mentioned, indexing of arrays begin at 0, not 1. Moreover, it end in N - 1, thus array[N - 1] yields the last element of the array. In your case N is 30.
    So, we use to access arrays like this:
    Code:
    for(i = 0 ; i < N ++i)
        array[i]...
    where N is the size of the array.
    Otherwise, that is how you have it (starting from one and ending at 30), will send you out of array bounds, which is a severe logical error, but quite common and easily traceable, as you gain experience
    Quote Originally Posted by Salem View Post
    > I guess I can't use global variables as function arguments?
    Of course you can, you just need to fix a bunch of other errors to begin with.
    It is a good practice to getting into the habit of not using global variables, in order to avoid conflicts and variable shadowing in the future, when your code is going to be of a BIG number of lines and the programmers reading and modifying it are going to be members of a team.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global linked list
    By PickleBranston in forum C Programming
    Replies: 13
    Last Post: 11-18-2010, 11:07 AM
  2. Global linked list
    By PickleBranston in forum C Programming
    Replies: 1
    Last Post: 11-17-2010, 10:21 AM
  3. Replies: 11
    Last Post: 04-17-2008, 02:29 AM
  4. Creating a global linked list?
    By TwistedJester in forum C Programming
    Replies: 1
    Last Post: 11-12-2007, 03:23 AM
  5. Initializomg a global variable from a data file
    By cybernike in forum C++ Programming
    Replies: 4
    Last Post: 06-27-2007, 01:07 AM