0

I have a 2d array of pointers (to strings)

 char *result[7000][14];

I want to write a function that returns the first string in each "row".

Here's what I tried:

char *getRownames (int a, int b, char *matrix[a][b])
{
    char *rownames[a];
    for(int i=0;i<a;i++){
        rownames[i] = malloc(strlen(matrix[i][0])+1);
        strcpy(rownames[i],matrix[i][0]);
    }

    return *rownames;
}

And then

 char *names = getRownames(7000, 14, result);

I get an error that says conflicting types for getRowNames. Still getting used to C and having to allocate my own memory.

JoshDG
  • 3,871
  • 10
  • 51
  • 85

2 Answers2

1

There is a couple of issues here

  • Your return statement is wrong (it should just be rownames, not *rownames). I wouldn't do it like that anyway.
  • I don't see the rest of your code, but if you don't initialize *result[][0], you will most likely segfault on the strlen call.
  • I would avoid trying to return a pointer to an array on the stack of that size (don't return pointers to local variables that have not been malloc'd), so I would pass in the array and have the function fill it out for you. If you had malloc'd a pointer to that size of data ie char *rownames=malloc(a*sizeof(char *)); you would be ok.

so I did this with my test code:

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

void getRownames (int a, int b, char *matrix[a][b], char* rownames[a])
{
    int i;
    for(i=0;i<a;i++){
        //printf("%d\n",strlen(matrix[i][0]));
        rownames[i] = malloc(strlen(matrix[i][0])+1);
        strcpy(rownames[i],matrix[i][0]);
    }
    //strlen(matrix[i][0])
    //return &rownames[0];
}

int main(void) {
    char *result [700][14];
    int i=0;
    for(i=0;i<700;i++){
    result[i][0]="abcd0";
    }
    char *rownames[700];
    getRownames(700,14,result,rownames);
    printf("I finished");
    printf("%s",rownames[0]);
    printf("%s",rownames[1]);
    printf("%s",rownames[2]);
    printf("%s",rownames[3]);
}
Youssef G.
  • 617
  • 4
  • 10
0

You have a few things going on here.

Function declarations/prototypes need to have fixed sizes for their arrays & matrices.*

char *getRownames (int a, int b, char *matrix[a][b])

won't work because the compiler doesn't know a or b when compiling your program. It would need to be

char *getRownames (int a, int b, char *matrix[7000][14])

if you know the array will be that size. Then you don't need a or b at all. If you want to be able to pass matrices of varying sizes to the function, that's a different matter entirely.

*(Note the compiler allows you to leave out the first dimension of arrays: char *matrix[][14] or char *array[])

Next, you'll need to cast the return value from malloc to a char*, as malloc() returns void*:

rownames[a] = (char*)malloc(strlen(matrix[i][0])+1);

By the way, it should be rownames[i] in your loop. :-) Since i is your loop variable.

And last, it looks like you want to return an array of char*, but return *rownames will send back just the first value in the array. Again, if you know the size the array will be, it's easier to pass an existing array to the function and have it fill in the values. Otherwise you have to malloc the array to return.

char *result[7000][14];
char *firstRows[7000];
//... other code that fills in these values
getRownames(7000, 14, result, firstRows);

void getRownames (int a, int b, char* matrix[7000][14], char* returnrows[7000])
{
    for(int i=0;i<a;i++){
        returnrows[i] = (char*)malloc(strlen(matrix[i][0])+1);
        strcpy(returnrows[i],matrix[i][0]);
     }
}
Marc
  • 324
  • 1
  • 9