Thread: How to access a array in a struct?

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    32

    How to access a array in a struct?

    I have written code to access array element in structure
    Code:
    #include<stdio.h>
    
    struct s
    {
      int A[10]; //structure member 
    }v;
    
    
    int main ()
    {
     int i = 0;
     
     struct s array[5] = {11, 12, 13, 14,15};
     
     for (i=0; i<5; i++)
     {
    	 printf("%d \n", array[i]);
     }	 
      return 0;
    }
    11
    0
    0
    0
    0


    Can someone tell me what's wrong with code?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,172
    A better compiler would point you in the right direction.
    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:13:22: warning: missing braces around initializer [-Wmissing-braces]
      struct s array[5] = {11, 12, 13, 14,15};
                          ^
    foo.c:13:22: note: (near initialization for ‘array’)
    foo.c:17:13: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘struct s’ [-Wformat=]
          printf("%d \n", array[i]);
                 ^
    $
    Each array needs a [ ]
    Each struct needs a .

    So perhaps
    array[i].A[j]

    Where j is another loop inside the loop you already have.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    32
    Quote Originally Posted by Salem View Post
    A better compiler would point you in the right direction.
    So perhaps
    array[i].A[j]

    Where j is another loop inside the loop you already have.
    I was just trying to acess the array (structure member)

    Am I really following good example ?

    Code:
    #include<stdio.h> 
    struct s
    {
      int A[10]; //structure member 
    }v;
     
     
    int main ()
    {
     int i = 0;
      int j = 0; 
     struct s array[5] = {11, 12, 13, 14,15};
      
     for (i=0; i<5; i++)
     {
    	 for (j=0; j<5; j++)
         printf("%d \n", array[i].A[j]);
     }   
      return 0;
    }
    11
    12
    13
    14
    15
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0

  4. #4
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    307
    Your code tries to create an array of your structure where each structure object holds an an array. What you're intending to do is creating one structure object and accessing its elements (in this case: the array that each object hold). So, instead of declaring an array of structure objects, declare one object and access its array.

    If you want to use an array of structure objects with initializer lists, you should be doing something like this:

    Code:
    struct UseMoreSensibleNames
    {
          // ...
    };
    
    // main
    struct UseMoreSensibleNames ArrayOfStructureObjs [3] = {{1,2,3,4,5,...} , {1,1,1,1,1,...} , {5,4,3,2,1,...}};
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't seem to access the Struct within my vector
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-21-2009, 08:37 PM
  2. struct access
    By complicato in forum C Programming
    Replies: 10
    Last Post: 04-21-2008, 02:54 AM
  3. How to access members of a struct
    By Bnchs in forum C Programming
    Replies: 9
    Last Post: 03-25-2008, 02:28 PM
  4. Using pointers to access something in a struct
    By Wiretron in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:14 AM
  5. How to access struct fields in array
    By dv007 in forum C++ Programming
    Replies: 3
    Last Post: 01-18-2006, 09:51 AM

Tags for this Thread