Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm trying to initialize char and int array in this simple code but I have error ,
n.c:25:19: error: expected expression before ‘]’ token
   select.arr1[]={
                   ^
n.c:31:18: error: expected expression before ‘]’ token
  select.num1[]={
                  ^
n.c:36:17: error: expected expression before ‘]’ token
 select.arr2[]={
                 ^
n.c:41:18: error: expected expression before ‘]’ token
  select.num2[]={


how can solve it please ?

What I have tried:

int main( void )
{
   
 struct str 
  {
   char arr1[4],arr2[4];
   int num1[4],num2[4];

 
} ;
struct str select ;

  select.arr1[]={
    { .arr1 = "lely" },
    { .arr1 = "sara" }
    
};

 select.num1[]={
    { .num1=30 },
    { .num1=70 }
    
};
select.arr2[]={
    { .arr2 = "sam" },
    { .arr2 = "denis" }
    
};
 select.num2[]={
    { .num2=50 },
    { .num2=90 }
    
};

printf("%s",select.arr1[0]);
printf("%s",select.arr1[1]);
printf("%d",select.num1[0]);
printf("%s",select.arr2[0]);


}
Posted
Updated 2-May-20 0:41am
v2
Comments
Lilyanaa 2-Jan-18 12:29pm    
yes,I saw it , but that link use array of struct
Dave Kreskowiak 2-Jan-18 12:29pm    
Your code doesn't make any sense at all and you have described what you're trying to do.

This appears to be homework, so I'm not going to give you what I suspect should the answer. If what i'm thinking is true, there are many things wrong with this code.
Lilyanaa 2-Jan-18 12:51pm    
No,it is not a homework, and in this code I'm trying to give constant value to the members of struct. thank you

Here is how initialize the structure :

struct str
{
   char arr1[4];   // this string is limited to three characters plus null
   char arr2[4];
   int  num1[4];
   int  num2[4];
};

struct str select =
{
    "Sam",
    "Joe",
    { 0, 1, 2, 3 },
    { 4, 5, 6, 7 }
};
The key things are arrays of characters can be initialized as an explicit array, like the integers, or as a string as I did above.
 
Share this answer
 
Comments
Lilyanaa 2-Jan-18 13:54pm    
Thank you so much, but if I have 3 string in arr1[] I know it must be define arr1[numofstring][stringlen] , but how can I give it initialization ?
You totally misunderstand the data layout of a struct. It has only the members you allocate.
C++
struct str 
{
   char arr1[4],arr2[4]; //both member have 4 chars (no char*)
   int num1[4],num2[4];  //both member have 4 integers
};
//this may work (I dont use that syntax
select = {
    { .arr1 = "lely" },
    { .arr2 = "sara" }
};
I would write plain code (or write a setter function)
C++
memset( &select, sizeof(select), 0 );// clear full struct
strncpy( select.arr1, 4, "lely" );

select.num[0] = 30;//access first field of array
select.num[1] = 50;//access secondfield of array
Use the debugger to see how the data in your struct gets changed.

You must first learn the basics. The linked tutorial looks fine for that.

Tip: use better names for code to understand it better.
 
Share this answer
 
Here is how initialize the structure :

struct str
{
   char arr1[4];   // this string is limited to three characters plus null
   char arr2[4];
   int  num1[4];
   int  num2[4];
};

struct str select =
{
    { 'S', 'a', 'm', 0 },   // or it could have been "Sam",
    { 'J', 'o', 'e', 0 },   // or it could have been "Joe",
    { 0, 1, 2, 3 },
    { 4, 5, 6, 7 }
};
The key things are arrays of characters can be initialized as an explicit array, like the integers, or as a string.
 
Share this answer
 
Comments
[no name] 2-Jan-18 13:47pm    
Looks like an accidental double post, because of the recent Connection Problems.
LOTS of problems.

1) You're defining a structure inside your Main method. This will make any instance of this structure only possible and only visible inside the Main method.

2) Those char arrays will only hold 4 characters, not the 5 and 6 character strings you have in your "initializers". Yes, there is another, invisible null character at the end of each string of characters.

3) You've got arrays for your integers, but that doesn't appear as what you're really trying to do. You seem to be trying to create an array of structure instances. You don't put the arrays inside the structure. You create an array of structures instead, each will have two strings of 3 characters or less and two integer values.

4) The prints at the end of the code are probably going to display something that you're not intending. You seem to be either wanting individual strings but you're getting partial strings.

5) In your "initializers", you cannot refer to variable names. The values you supply have to cover every field and be provided in the order that they show up in the structure:
str select2 = {
    "lely", "sara", { 30, 70, 20, 10 }, { 1, 2, 3, 4 }
};


6) When you use the struct keyword, you're defining a new structure. Your struct str select is a very bad attempt at re-defining the str structure.

And that's just for starters... Your code is so bad everywhere, it makes it impossible to try to figure out what you're really trying to do and what the structure should look like.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900