Thread: variable passing problem

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    6

    variable passing problem

    Here is was I am trying todo I am calling a function within a function and passing to it a few things an array, a couple of ints and a string I am getting an error on the string when I call it from the function and try to place it in a struct I have include the function I am having problems and the struct. The error I am getting is incompatible types in assignment. If anyone can explain my error to me because I can't find it I would be very greatful.
    I *'ed the lines that were popping up as errors
    Thanks

    Code:
    //struct to hold information from file
    typedef struct{
       int id;
       char name[50];
       int age;
    }record_t;
    the function
    Code:
    //inserts items into array of structs
    //Pre-Condition: fileptr points to a file and table
    //is an empty array
    //Post-Condition: table has element added as long as
    //table is already full
    void inserttable(int id, char name[50], int age, int firstloc,
       record_t table[41])
    {
       int index;
       index = firstloc;
       if (table[index].id == -1){
          table[index].id = id;
          table[index].name = name;*
          table[index].age = (int)age;
       }else{
          index = (index+1)%41;
          while(index != firstloc || table[index].id != -1){
            index = (index+1)%41;
          }
          if (index == firstloc){
             printf("Unable to add item array full.\n\n");
          }else{
             table[index].id = id;
             table[index].name = name;*
             table[index].age = (int)age;
          }
        }
    }
    Last edited by bsimbeck; 02-09-2003 at 12:40 PM.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You can't assign strings using the = operator. Strings aren't single values but an array of characters. Use strcpy() to copy a string.

    Also, I'd suggest you change char name[50] in your argument list into char* name to pass a pointer to the string (4 bytes) instead of passing the whole string (50 bytes).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I'd suggest you change char name[50] in your argument list into char* name to pass a pointer to the string (4 bytes) instead of passing the whole string (50 bytes).
    If your refering to this:
    >void inserttable(int id, char name[50], int age, int firstloc, record_t table[41])
    it wont make any difference. When you're talking arrays, you only pass a pointer to the array, not the array itself.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    *Making a test program*

    You're right. My fault...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing variable between cpp files
    By Todd88 in forum C++ Programming
    Replies: 28
    Last Post: 12-03-2008, 07:01 PM
  2. Problem with passing structs to a function.
    By darsh1120 in forum C Programming
    Replies: 7
    Last Post: 03-11-2008, 04:36 AM
  3. Need help passing a variable between forms
    By GUIPenguin in forum C# Programming
    Replies: 8
    Last Post: 07-10-2006, 03:13 AM
  4. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 01:50 AM
  5. Variable passing
    By seizmic in forum C++ Programming
    Replies: 2
    Last Post: 10-29-2005, 08:22 AM