Thread: how to assign value to structure array

  1. #1
    Registered User
    Join Date
    Feb 2022
    Posts
    73

    how to assign value to structure array

    Hi all, I'm having a little bit of trouble with my code about structures

    I have decleared structure and I want to initialize array in single statement

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    struct point 
    {
        int array[5];
    };
    
    
    int main()
    {
        struct point svar;
        struct point spvar;
        
        svar.array[5] = {1, 2, 3, 4, 5};
            
        return 0;
    }
    error

    Code:
     error: expected expression before '{' token
    svar.array[5] = {1, 2, 3, 4, 5}

    can anyone tell me what's the reason of error

  2. #2
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    You are setting the sixth element to a array.
    Code:
    svar.array[5] = {1, 2, 3, 4, 5};
    Try
    Code:
    svar.array = {1, 2, 3, 4, 5};
    My Bad.. Try this
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct point 
    {
        int array[5];
    };
    
    int main(int argc, char ** argv) {
        struct point svar = {{1,2,3,4,5}};
        struct point spvar;
         
        return 0;
    }
    Last edited by G4143; 03-01-2022 at 10:02 PM.

  3. #3
    Registered User
    Join Date
    Feb 2022
    Posts
    73
    Quote Originally Posted by G4143 View Post
    You are setting the sixth element to a array.
    Code:
    svar.array[5] = {1, 2, 3, 4, 5};
    Try
    Code:
    svar.array = {1, 2, 3, 4, 5};
    I tried that but I am getting same error. I have GCC compiler

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Unfortunately, you cannot assign to an array itself. However, you can initialise them, and in this case it means initialising the struct of which the array is a member:
    Code:
    struct point svar = {1, 2, 3, 4, 5};
    or if you want to consider that the struct may have more members:
    Code:
    struct point svar = {{1, 2, 3, 4, 5}};
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2022
    Posts
    73
    Quote Originally Posted by laserlight View Post
    if you want to consider that the struct may have more members:
    Code:
    struct point svar = {{1, 2, 3, 4, 5}};
    Code:
    #include<stdio.h>
    
    #include<stdlib.h>
    
    
    struct point 
    {
        int array[5];
    };
    
    
    
    
    int main()
    {
       int i;
       struct point *spvar; 
       struct point svar = {{1, 2, 3, 4, 5}};
       
       spvar = &svar;
       
       for ( i = 0; i < 5; i++)
       {
    	   printf( "%d ", *spvar );
    	   spvar++;
       }
       
        return 0;
    }
    when code run it gives output 1 6422296 4199048 0 4199157 and I was expecting 1, 2, 3, 4, 5

  6. #6
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    You have to understand C and pointers..
    Code:
    struct point *spvar;
    This pointer is not pointing at an integer, its pointing at a struct. Just for your benefit. Try taking the sizeof(int) and the sizeof(struct point).

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Dadu@
    when code run it gives output 1 6422296 4199048 0 4199157 and I was expecting 1, 2, 3, 4, 5
    spvar is a pointer to struct point. Therefore, *spvar is a struct point. So what you're doing here:
    Code:
    printf( "%d ", *spvar );
    is trying to print a struct point object as if it were an integer. It isn't. Your compiler should have warned you about that, and if it didn't, you need to compile at a high warning level and pay attention to warnings.

    What you want to do is something like this:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    struct point
    {
        int array[5];
    };
    
    
    int main(void)
    {
        struct point svar = {{1, 2, 3, 4, 5}};
    
        for (size_t i = 0; i < 5; i++)
        {
            printf("%d ", svar.array[i]);
        }
        printf("\n");
    
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assign values 2d array to normal array
    By Gertjan Haan in forum C Programming
    Replies: 1
    Last Post: 10-29-2015, 11:10 PM
  2. How can I assign string Hello to structure member?
    By bilsch02 in forum C Programming
    Replies: 5
    Last Post: 02-17-2015, 07:18 PM
  3. assign string to the string variable inside structure!
    By king_zart in forum C++ Programming
    Replies: 20
    Last Post: 12-09-2012, 11:37 AM
  4. Assign array to another.
    By boblettoj99 in forum C Programming
    Replies: 8
    Last Post: 12-03-2009, 04:32 PM
  5. Replies: 5
    Last Post: 07-28-2007, 04:03 AM

Tags for this Thread