0

I'm trying to send a file to a function that creates a matrix of a struct called Entry, populates it based on the file, and returns the matrix. Right now it's a cascade of errors that I can add if needed, but I suspect I'm just making multiple dumb errors. If there's an easier way that doesn't involve malloc, that would work just as well.

What am I doing wrong and how can I do this properly?

Here's the relevant code:

Entry **matCreate(FILE *fp){

// initialize matrix
Entry matrix = malloc(states * N_CC * sizeof(Entry));

// populate the matrix with initial values
for (int i = 0; i < states; i++) {
  for (int j = 0; j < N_CC; j++) {
    matrix[i][j].next = 99;
    matrix[i][j].action = "D";
  }
}
return matrix;
}

and in the main function:

Entry **matrix = matCreat(fp);

I'm starting the function off with

Entry **matCreate(FILE *fp){

Initializing it with

Entry (*matrix)[N_CC] = malloc( states * sizeof *matrix );

Populating it with:

for (int i = 0; i < states; i++) {
  for (int j = 0; j < N_CC; j++) {
    matrix[i][j].next = 99;
    matrix[i][j].action = "D";
  }
}

And getting it from main with: Entry ourMat = (*matCreate(fp))[N_CC];

But still getting the error: tokenize.c:84:1: warning: return from incompatible pointer type [enabled by default]

Where am I going wrong?

10

2 Answers 2

2

C doesn't have a true multidimensional array type. You have to either return a jagged array, or return an array of arrays. (Also, since arrays cannot be returned by value; in both cases you have to dynamically allocate the array, and return a pointer to it). Further reading.

For the array of arrays option, you declare and allocate it like this:

Entry (*matrix)[N_CC] = malloc( states * sizeof *matrix );

By using this pattern for the argument to malloc, you avoid having to worry about any complicated stuff.

The type of a function to return this pointer is:

Entry (*matCreate(FILE *fp))[N_CC];

In the calling code you can use the function like this:

Entry (*matrix)[N_CC] = matCreate(fp);
bar( matrix[1][2].action );

You could use a typedef to make all of this simpler syntax. Personally I prefer not to, as then it is less obvious when array-pointer decay is happening.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I confused myself. If I may ask one more question, am I still able to access elements at, for example, "ourMat[0][0].action"? I'm getting the error "subscripted value is neither array nor pointer nor vector".
2

You need to assign malloc return pointer to Entry **

  Entry **matrix;
  matrix= (Entry**)malloc(states * sizeof(Entry *));
  for (i = 0; i < states; i++)
       Entry[i] = malloc(N_CC *   sizeof(Entry));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.