0

I have this code:

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

static char** testing(FILE *fp)
{ 
    char temp[255];
    char data[255][255]; 
    for (int i = 0; !feof(fp); i++)
    {
        fgets(temp, 255, fp);
        strcpy(data[i], temp);
    }
    
    for (int i = 0; i < 66; i++)
    {
        printf("%s", data[i]);
    }

    return data;
}

int main(int argc, char const *argv[])
{
    FILE *fp; 

    fp = fopen(argv[1], "r");
    testing(fp);
}

I want to return the 2D array data but when I compile this, I get the output:

returning 'char (*)[255]' from a function with incompatible return type 'char **' [-Wincompatible-pointer-types] return data;

I don't see what I've done wrong.

Any help would be very much appreciated.

Slash
  • 31
  • 6
  • 5
    Ignoring the compiler error your function in any case is incorrect because it tries to return a pointer to the local array char data[255][255];. – Vlad from Moscow May 14 '21 at 20:30
  • 1
    As for the compiler error then the return type of the function and the type of the returned expression are different and are not compatible. – Vlad from Moscow May 14 '21 at 20:31
  • You need dynamically to reallocate an array of strings within the function and return it. – Vlad from Moscow May 14 '21 at 20:35
  • And the condition in the for loop is also incorrect. Due to the condition the last line of the file can be present twice in the result array. – Vlad from Moscow May 14 '21 at 20:37
  • And it is unclear how you obtained the magic number 66 used in the second for loop.:) – Vlad from Moscow May 14 '21 at 20:38
  • The compiler showed you what is the return type of the function and what is the type of the returned expression. You can see the difference between the types in the error message. – Vlad from Moscow May 14 '21 at 20:42
  • @VladfromMoscow thanks for all the help. Im new to pointers so I suppose this is to be expected. This is just a protype code I wrote in a minute just to test out the 2D array so I just wrote a random number `66` for the loop and same for the first for loop. Thanks again – Slash May 14 '21 at 20:44
  • `char [255][255]` is completely different from`char**`. these types are not compatible in any way and you cannot mix them. the compiler is telling you that. – n. 'pronouns' m. May 14 '21 at 20:47
  • TLDR: [`char **` does **NOT** refer to a "two-dimensional array"](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays) When you've been told that `char **` refers to a "two-dimensional array", you were told wrong. In that context, `char **` refers to a ***one***-dimensional array of pointers to **multiple and separate** one-dimensional `char` arrays. That's not a "two-dimensional array". – Andrew Henle May 14 '21 at 21:17
  • And this is appropriate for your `for (int i = 0; !feof(fp); i++)` loop: [**Why is “while ( !feof (file) )” always wrong?**](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Andrew Henle May 14 '21 at 21:20

3 Answers3

2
  1. data is two-dimensional array of chars. When 2D array decals to a pointer it has the type of pointer to the char array.

the function should be declared as :

static char (*testing(FILE *fp))[255]
  1. Returning the pointer to local object is dangerous as dereferencing the returned pointer invokes Undefined Behaviour. The local automatic variables stop existing when a function returns. You need to use (a)global variables, (b)static variables or use (c)malloc family functions to allocate the memory.

(a):

char data[255][255]; 
static char (*testing(FILE *fp))[255]
{
     /* ... */

(b):

static char (*testing(FILE *fp))[255]
{
    static char data[255][255]; 
     /* ... */

(c):

static char (*testing(FILE *fp))[255]
{
    char (*data)[255] = malloc(255 * sizeof(*data)); 
     /* ... */
0___________
  • 34,740
  • 4
  • 19
  • 48
0

Your data variable is not allocated dynamically and therefor cannot be used outside your function. You need to return a pointer to a dynamically allocated array to use it outside of it.

Here for more details about heap and stack:

SamHuffman
  • 13
  • 3
-1

You declared the return value to be a char** which can be interpreted by reading from right to left, so is a pointer to a pointer of type char. While data is a two dimensional array. Returning data as you do, is returning the pointer to data, which the compiler sees as a pointer to a one-dimensional array. That is why you got an incompatible data error. Additionally you have issue with using a locally declared variable which is not visible outside the function so the value returned would result in a protection fault.

  • compiler does not pointer as an array. Arrays and pointers are something completely different – 0___________ May 14 '21 at 22:20
  • In C, arrays and pointers are different but related. An array is a memory location that maybe accessed by utilizing its index. The compiler treats an array variable WITHOUT it's index as the address of the beginning of the first item in the array. A pointer is simply an address in memory. We can declare a pointer variable and access the data through that as below: char array[7] "Cheese/0"; char* ptr = array; char ch = &(ptr+1); We can manipulate ptr to access any item in the array. – TImothy K. Worrell May 15 '21 at 01:04