0

I have defined a structure for users as USER:

struct user {
    char *name;
    int age;
};

typedef struct user USER;

And I can create an array of users:

USER uArray[3] = {
    { "Allan", 12 },
    { "Bob", 34 },
    { "Chris", 56 }
};

Now how do I go about adding a predefined user to a list of users?:

USER u1 = { "Dave", 78 };

I assumed that you could just add it to the list as followed. However, an error is returned, stating that it cannot convert from 'USER' to 'char*'.

USER uArray[4] = {
    { "Allan", 12 },
    { "Bob", 34 },
    { "Chris", 56 },
    u1
};

I understand that its treating u1 as the first element of creating a user (name = u1) so how else can you add a predefined user to an array of users?


The code is now working:

#include <stdio.h>

struct user {
    char *name;
    int age;
};

typedef struct user USER;

void changeName(USER *u);

void main(void)
{
    USER u1 = { "Dave", 78 };

    USER uArray[4] = {
            { "Allan", 12 },
            { "Bob", 34 },
            { "Chris", 56 }
    };

    // add u1 to position 3 of the array
    uArray[3] = u1;

    for (int i = 0; i < 4; i++)
    {
        if (uArray[i].name == "Bob")
        {
            changeName(&uArray[i]);
        }
        printf("User: %d, Name: %s, Age: %d\n", i, uArray[i].name, uArray[i].age);
    }
}

void changeName(USER *u)
{
    u->name = "Dave";
}
14
  • That adds it to the array, but when executed, it returns a debug error; stating that the stack around the variable (both uArray and u1) was corrupted. Commented Sep 24, 2014 at 10:24
  • use C99 Compiler. E.g clang, gcc. Commented Sep 24, 2014 at 10:24
  • Is it something to do with the compiler? I mean should the provided solution work? Commented Sep 24, 2014 at 10:27
  • @Kurtiss variable can be used in the initialization since c99.(except in the case of like global variable) Commented Sep 24, 2014 at 10:36
  • Do you have braces around the u1 in your code? That would at least explain the error about converting from USER to char. The stack corruption happens probably elsewhere, so you should post more code. (If the program is large, try to isolate the error in a few lines and post only these.) Commented Sep 24, 2014 at 10:41

1 Answer 1

1
 #include <stdio.h>

   struct user {
      char *name;
      int age;
   };

   typedef struct user USER;

  USER u1 = { "Dave", 78 };

  USER uArray[4] = {
     { "Allan", 12 },
     { "Bob", 34 },
     { "Chris", 56 }
  };

  int main()
  {

     int i;

     uArray[3] = u1;
     for (i = 0; i < 4; ++i)
     {
        printf("%s: %d\n", uArray[i].name, uArray[i].age);
     }
     return 0;
  }
Sign up to request clarification or add additional context in comments.

5 Comments

That adds it to the array, but when executed, it returns a debug error; stating that the stack around the variable (both uArray and u1) was corrupted.
I compiled and got proper output. cc <filename.c> -o f1 ./f1
Ok its been fixed, however the solution confuses me; when the array is defined, you have to specify the maximum number of users allowed within the array (4) but to add the predefined user to the array, you have to specify how many users the array is currently holding (3) and not only that, but it looks like the solution states that the array is equal to the user, where in fact, its just adding the user to the list, wth?
@Kurtiss: That's C for you. You should probably keep a count of active users alongside the array and take care not to overflow the array. By the way, uArray[3] = u1 does quite explicitly assign the user u1 to the fourth entry (with index 3) of the array of users. It doesn't say that the array is equal to the user.
I just realised that now >< so it's assigning the user to that specific point within the array (position 3 in this case), sorry, I can be so stupid sometimes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.