0

I have a struct:

typedef struct myStruct {
  char** a;
  char** b;
} myStruct;

I am trying to read from stdin and initialize an array of myStruct*

int main() {
  int maxlength = 60 + 1;
  int arraySize = 2;
  myStruct** myArray = (myStruct*) malloc(sizeof(myStruct*) * arraySize);
  int runningIndex = 0;
  while(1) {
    char* aline = (char*) malloc(maxlength);
    int status = getline(&aline, &maxlength, stdin);
    if(status == -1)
      break;
    char* bline = (char*) malloc(maxlength);
    getline(&bline, &maxlength, stdin);
    if(runningIndex == arraySize) {
      arraySize *= 2;
      myArray = realloc(myArray, sizeof(myStruct*) * arraySize);
    }
    myArray[runningIndex] = (myStruct*) malloc(sizeof(myStruct*));
    myArray[runningIndex]->a = &aline;
    myArray[runningIndex]->a = &bline;
    runningIndex++;
  }
  for(int i = 0; i < runningIndex; i++) {
    printf("outside the loop at index %d, a is %s and b is %s", i, *myArray[i]->a, *myArray[i]->b);
  }
}

I did a few printf within the while to confirm that each myStruct is successfully created with the strings from stdin. However, outside the loop, all the stored values seems to be gone. I was thinking about scope but could not figure out why. Could someone explain how I shall do this properly? Thanks!

Ra1nWarden
  • 1,170
  • 4
  • 21
  • 37

2 Answers2

0

A single string pointer is defined like this:

char *pStr;

An array of string pointers is defined like this:

char **pStrArr;

To dynamically create memory for a single string, do this:

pStr = malloc(STRING_LEN);

To dynamically create memory for an array of strings, do this:

pStrArr = (NUM_STRINGS * (*pStrArr));
for(str = 0; str < NUM_STRINGS; str++)
    pStrArr[str] = malloc(STRING_LEN);

In your case, you'd need to create either two char strings for your struct or two arrays of char strings, whatever your requirements are. It looks like you really only need two single strings. If so, do this:

typedef struct
{
    char* a;
    char* b;
} myStruct;

myStruct structure;

structure.a = malloc(STRING_LEN);
structure.b = malloc(STRING_LEN);
Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
0

sample to fix :

#include <stdio.h> 
#include <stdlib.h>

typedef struct myStruct {
    char *a;
    char *b;
} myStruct;

int main() {
    int maxlength = 60 + 1;
    int arraySize = 2;

    myStruct* myArray = malloc(sizeof(myStruct) * arraySize);
    int runningIndex = 0;
    while(1) {
        char *aline = malloc(maxlength);
        int status = getline(&aline, &maxlength, stdin);
        if(status == -1)
            break;
        char *bline = malloc(maxlength);
        getline(&bline, &maxlength, stdin);
        if(runningIndex == arraySize) {
            arraySize *= 2;
            myArray = realloc(myArray, sizeof(myStruct) * arraySize);
        }
        myArray[runningIndex].a = aline;//&aline is address of local variable.
        myArray[runningIndex].b = bline;//And content is rewritten in each loop.
        runningIndex++;
    }
    for(int i = 0; i < runningIndex; i++) {
        printf("outside the loop at index %d, a is %s and b is %s", i, myArray[i].a, myArray[i].b);
    }
    //deallocate
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70