2

I have a structure as follows

struct a
{
    char* ap;
    char* inputs[10];
    int e;
};

then I have created an array of this structure like struct a list [100];

now i want to fille the array inputs[10] and for that I am using the syntax to access the first location :

    ip=0;
    inp=0;
    list[ip].inputs[inp]

but I am gettin the error "error C2107: illegal index, indirection not allowed" on compiling the code

please suggest how to access the array location inside array of structure.

regards
priya 
AurA
  • 12,135
  • 7
  • 46
  • 63
priya
  • 852
  • 18
  • 39

3 Answers3

1

Working fine on my pc.. here is my code..

#include <stdio.h>

struct a
{
char* ap;
char* inputs[10];
int e;
};

int main()
{
    int ip=0;
    int inp=0;
    struct a list[100];
    list[ip].inputs[inp] = 'A';
    printf("This: %c", list[ip].inputs[ip]);
    return 0;
}

OUTPUT= This: A

let me know whether it helped or not..

Hiren Pandya
  • 989
  • 1
  • 7
  • 20
  • actually i think there is a mismatch in the l value and r value in my case as i am using following syntax to fill the array: list[ip].inputs[inp]= (char*)malloc(((int) strlen((char*)key)+1)); – priya Apr 05 '13 at 07:03
1

Here you use array of character pointer in your structure. So Initially you allocate memory for you structure by creation list of 100. I think you didn't create memory for you array of character pointer. You have to create memory for each of character pointer. So I suggest example code.

#include <stdio.h>

struct a
{
char* ap;
char* inputs[10];
int e;
};

int main()
{
    int ip=0;
    int inp=0;
    struct a list[100];
    list[ip].inputs[inp]= (char*)malloc(25);
    scanf("%s",list[ip].inputs[inp]);//Or other copy function to fill string
    printf("output %s",list[ip].inputs[inp]);
}
Mani
  • 17,549
  • 13
  • 79
  • 100
  • hi i am allocating the memory to this structure using the following code : list[ip].inputs[inp]= (char*)malloc(((int) strlen((char*)key)+1)); and then copying the value using following : strcpy(list[ip].inputs[inp],(char*)key); but getting an error on the line where i am allocating the memory – priya Apr 05 '13 at 07:09
  • @priya which error you got.? In first point , you told about creating memory for structure. that's not correct. You are creating memory for structure element.(inputs variable which is structure element which is not point to whole structure).. – Mani Apr 05 '13 at 08:20
0

The struct themselves do not have data. You need to create objects of the struct type and set the objects ...

     struct a
        {
        char* ap;
        char* inputs[10];
        int e;
        };

/* I like to separate the type definition from the object creation */
struct a list [3];


list [0].inputs[0] = "Choclate";
list [0].inputs[1] = "Almond";
list [0].inputs[2] = "Rasberry";

Hope it ll usefull. Also refer this article

Community
  • 1
  • 1
AB Bolim
  • 1,997
  • 2
  • 23
  • 47