简体   繁体   中英

Undefined behavior with 2d array of struct C

I have a 2d array of structs that I am assigning strings to, here is my struct.

struct node {
  char* value;
};

Here is my allocation (I am new to C so I am not sure if it is right) but there will always be 35 columns but there could be millions of rows.( I just had it at 3 for now for testing)

const int rows=3;
    struct node ** arrayofnodes[rows][35];
for(int i=0; i<rows; i++) {
    array[i] = malloc(test * sizeof array[0]);
    for(int j=0; j<35; j++) array[i][j] = malloc(sizeof array[0][0]);
}

I then read in character by character from a csv file and have a temp string, and assign the value of the temp to the position I want by using this below.

//int row and count are defined in my while loop I have for counting commas(or what col I am on) then new lines for the rows 
arrayofnodes[row][count]->value=strdup(temp);
   printf("%s  \n", arrayofnodes[row][count]->value);
   printf("%d %d \n",row, count );

When I assign like the way above it seems to work. I added these print statements in to make sure it was assigning the right values.

For example above would print out something like

 Red 0 0 

And this is correct for that position.

But then after I do all of my assigning. I placed a print statement printf("%s \\n", arrayofnodes[0][0]->value); to test if I can retrieve the 1st value as shown above which should be "Red".

In my terminal it outputs "@`??" or "@Pz?" or just any random output. I have tried this for a bunch of different positions besides 0,0, but they all get the same outcome. I guess I am just confused why the print statements work right after I assign them, but not at the end of my code when I call them later.

You can avoid overcomplicated allocation by using constant size inside the structure:

struct OneRow
{
    char Value[35];
}

const int Rows=3;

OneRow *MyArray=NULL;

MyArray = (OneRow*) malloc (Rows*sizeof(OneRow));

You can now access each element (character) or a whole string as

MyArray[rownumber].Value[colnumber] = …
strcpy (MyArray[rownumber].Value, "I'm_shorter_than_35"); //34 chars max + null-term

This is what it looks like you're trying to do. You will need to scan your csv file and compute the number of rows required, then populate the values however you want.

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

struct node {
  char* value;
};

int main() {
  const int rows = 3; // you will need to compute this beforehand
  const int columns = 35;

  struct node** arrayofnodes = malloc(rows * sizeof(struct node*));

  for (int i = 0; i < rows; ++i) {
    arrayofnodes[i] = malloc(columns * sizeof(struct node));
  }

  for (int i = 0; i < rows; ++i) {
    for (int j = 0; j < columns; ++j) {
      arrayofnodes[i][j].value = malloc(...);
      strcpy(arrayofnodes[i][j].value, ...); // etc..
    }
  }

  for (int i = 0; i < rows; ++i) {
    for (int j = 0; j < columns; ++j) {
      free(arrayofnodes[i][j].value);
    }
  }

  for (int i = 0; i < rows; ++i) {
    free(arrayofnodes[i]);
  }

  free(arrayofnodes);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM