3

I usually deal with array of strings this way, because it allow me to not specify a character limit :

char *names[2] = {"John","Doe"};
printf("%s\n",*((names)));
printf("%s\n",*((names)+1));

I am unable to reproduce this while using a struct.

I tried with both john.names = {"John","Doe"}; and john.*names = {"John","Doe"}. But I am getting an expected expression error.

However, I am able to do so during the initialization with Person john = {{"John","Doe"}};. So I'm not sure if it's allowed to proceed like this only during initialization.

main.h

typedef struct Person Person;

struct Person
{
    char *names[2];
};

main.c

#include <stdio.h>
#include <stdlib.h>
#include "main.h"
int main()
{
    Person john = {{"John","Doe"}};
    john.names = {"John","Doe"}; // Expected expression error
    printf("%s\n",john.names[0]);
    printf("%s\n",john.names[1]);
    return 0;
}

What would be the "expected expression", am I allowed to do this ?

1
  • @xing yes of course, I am using this to printf just before my return 0. But what I would like is being able to use brackets. Commented Jan 14, 2019 at 17:03

3 Answers 3

4

Arrays are not first class elements in C. You can initialize a full array, but you can only assign to non array elements, meaning scalars, pointers or structs.

When you write

char *names[2] = {"John","Doe"};

, it is an initialization, not an assignment. And the following assignment would also choke with a syntax error:

char *names[2];
names = {"John","Doe"};    // syntax error here
Sign up to request clarification or add additional context in comments.

Comments

3

For initialization of a specific field/attribute, you can do something like the following during declaration:

Person john = {.names = {"John","Doe"}};

To initialize additional fields, let's say address and names, this can be done like the following:

Person john = {.names = {"John","Doe"}, .address="foo"};

After declaration, you will have to specify the array index.

john.names[0] = "John"

3 Comments

"After declaration, you will have to specify the array index." So from what I understand, there is no way to use brackets after initialization ?
To populate multiple indices? i.e. 0,1 at the same time? No.
@shellwhale: C does not support array assignment — and john.names = … is attempting to assign to an array.
2

Well , i guess that there are may ways to access the content of a struct , the way i learned it on my side is for a structure looking like that //

typedef struct struc_person { char *name[2]; } type_person;

type_person->name[0] = value_of_the_first_name;

type_person->name[1] = value_of_the_surname;

1 Comment

That depends what type_person represents in your assignment example: if it's a pointer to the type_person that you've typedeffed then yes you would use -> but if it's an instance of the structure then it's .. As written this wouldn't compile, but if you meant type_person to be an instance of type_person then you need to use . not -> here.

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.