0
votes

With your help here in this site (the question is here), I've created a struct pointer as following:

struct myStruct {
   char charVar;
   int intVar;
};
int indexnum = 1;
struct myStruct *p = NULL;
p = malloc(sizeof *p);
p[indexnum].member = x;
index++;

everytime I add a new struct, I increase 'indexnum' so I give different names for each struct.

"mystruct" is global, but the declaration "struct myStruct *p" is in the main function.

the problem is, I cannot reach that p structs from other functions. I just couldn't figure out how to.

The following code is just one of the many trials,

void func1 (char str1, int index, int num1){
int i=0;
struct myStruct *p = NULL; 
p[i].charVar="adadsasd";

When the debugger reaches this last line, I get this error: Unhandled exception at ...... Access violation writing location

What should I do?

3
Your malloc is strange, index have to start with 0, char-arrays are not chars, and have to be assigned with strcpy etc. - deviantfan
malloc suggestion was given here. - user3108849

3 Answers

3
votes

It looks like you are trying to create an array of struct myStructs. There are a few ways to do so. The first is to allocate the structs on your stack ala:

struct myStruct p[N];

where N is some positive integer value. Note that by declaring this array on the stack, once your program leaves the scope of wherever this array was declared in, the array and all its contents become lost. Example:

struct myStruct foo(int N) {
    int i;
    struct myStruct p[N];
    for(i = 0; i < N; i++)
        p[i].intVar = i;

    for(i = 0; i < N; i++)
        //Prints value of intVar of each struct myStruct on a different line
        printf("%d\n", p[i].intVar); 

    // Returns p but the moment the scope returns to the main function, p
    // ceases to exist
    return p;
}

int main() {
    struct myStruct p[10] = foo(10);

    // This gives you an error as the program has no notion of p outside the scope
    // of foo
    printf("%d\n", p[5].intVar);
}

The other option is to allocate the array onto the heap ala:

struct myStruct **p = malloc(N * sizeof(struct myStruct *));

Note that constructing an array like this means you have to also allocate space on the heap for each struct in the array. Example:

struct myStruct** foo(int N) {
                      struct myStruct **p = malloc(N * sizeof(struct myStruct *));
                      int i;
                      for(i = 0; i < N; i++)
                          p[i] = malloc(sizeof(struct myStruct));

                      return p;
                   }

int                main() {
                       struct myStruct **p = foo(10);
                       p[5]->intVar = 10;

                       // This is now valid because p and its elements
                       // exist on the heap until you free them
                       printf("%d\n", p[5]->intVar);

                       // For every malloc you must have a free, lest
                       // you run into memory leaks
                       int i;
                       for(i = 0; i < 10; i++)
                           free(p[i]);
                       free(p);

                   }
1
votes

You need to redefine your struct as

struct myStruct {
   char* charVar;
   int intVar;
};

So that charVar is a pointer to a string. Then when you want to set the string value, you can use strcpy. Also you need to malloc for each charVar with NUM_CHARS being the maximum length of each string.

for(i=0; i < num_of_elements; i++) {
    p[i].charVar = (char*) malloc(sizeof(char)*NUM_CHARS);    
    strcpy(p[i].charVar, "adadsasd");
}

Also note that when you allocate for p, you need to do the following:

struct myStruct *p;
p = (struct myStruct*) malloc(sizeof(struct myStruct)*num_of_elements);

so that p is a pointer to a struct array of type myStruct.

0
votes

If you want to change the value of 'p' pointer from another function this can be done by passing pointer to pointer as function parameter

The code sample is as follows:

void test(int **q){
          *q = malloc(sizeof(int));
          **q = 1;
   }        

   int main(){
            int *p;
          test(&p);
          printf("%d", *p);
          free(p);
          return 0;
  }