Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

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 
share|improve this question
    
Have you tried with the pointer.. I mean *list and then list[ip]->inputs[inp]..?? – Hiren Pandya Apr 5 '13 at 6:22
    
yes I have tried but still the same error – priya Apr 5 '13 at 6:27
1  
Works here without error. Maybe an MSVC bug? – user529758 Apr 5 '13 at 6:31

3 Answers 3

up vote 1 down vote accepted

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]);
}
share|improve this answer
    
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 5 '13 at 7: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 5 '13 at 8:20

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..

share|improve this answer
    
The purpose of char * is store/point a string – Alter Mann Apr 5 '13 at 7:02
    
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 5 '13 at 7:03

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.