-4

I'm writing a function which is to read in a file and insert the text character by character into an array, and then return a pointer to that array. The max size permitted for the file is supposted to be 2KB.

Here's what I have now:

int main(){
    char data[2048];
    char* data4=layer4(data);
}

char* layer4(char array[]){
    FILE *fp;
    fp=fopen("sendfile.txt","r+");
    fscanf(fp, "%c", array);
    for(int i=0; i<2048; i++){
        printf("%c\n",array[i]);
    }
    return &array;
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
freelancer05
  • 77
  • 2
  • 6
  • Why? what you want to achieve by returning? – Sourav Ghosh Feb 09 '16 at 20:58
  • 1
    The array is going to be used in other function calls to come. – freelancer05 Feb 09 '16 at 20:59
  • @freelancer05: you needn't return the array, the main knows about it, and it's modified in-place. –  Feb 09 '16 at 21:00
  • @freelancer05, don't believe the answers below. They are not going to help you, as the problem you have is not in returning something, but first reading into something. – Jay Kumar R Feb 09 '16 at 21:06
  • Why are you reading just 1 character from the file, then printing 2048 characters from the array? All but the first of them are uninitialized. – Barmar Feb 09 '16 at 22:14
  • @YvesDaoust Why does `strcpy()` need to return its first argument? – Barmar Feb 09 '16 at 22:15
  • @Barmar: possibly because it allows to nest several calls, like strcpy(a, strcpy(b, c)). But it needn't do it as the returned value is the value passed. –  Feb 09 '16 at 22:39
  • @Barmar, why this function needs to return pointer is the problem. If you have question about strcpy, create a new post about it, or write to ANSI-C committee to change it. – Jay Kumar R Feb 10 '16 at 22:11
  • @JayKumarR That was a rhetorical question. My point is that it's reasonable for a function to return a pointer even when it's the same as the argument, because `strcpy()` is a model to emulate. – Barmar Feb 10 '16 at 22:17

6 Answers6

6

You only need to return:

return array;
  • But note that it isn't a pointer to an array, it is a pointer to `char`. The distinction is important. – juanchopanza Feb 09 '16 at 20:59
  • 1
    surprised how this wrong answer gets 3 '+' or 'ups'. The OP has no loop reading the file, scanf will read always into first element of the array. As Sourav Gosh pointed out, there is nothing to return anyway as parameter has the same pointer as return value. – Jay Kumar R Feb 09 '16 at 21:03
  • @Jay Kumar R `strcpy()` and many standard functions return the same pointer. Should the function return the value or not isn't OP's central issue. – chux - Reinstate Monica Feb 09 '16 at 21:10
  • @chux, exactly. Why do you talk about returning the value, while the code has not read more than 1 char from the file? Why everyone answers return value when central issue is something else? – Jay Kumar R Feb 10 '16 at 22:08
  • @Jay Kumar R I mentioned the return value as a comment to [your comment](http://stackoverflow.com/questions/35302052/how-to-return-a-pointer-to-an-array-in-c/35302076?noredirect=1#comment58314504_35302076) that mentioned the return value. Sounds like we agree the key issue is not the return value. – chux - Reinstate Monica Feb 10 '16 at 22:15
3

Multiple problems. Some implied by @Jay Kumar

char* layer4(char array[]){
    FILE *fp;
    fp=fopen("sendfile.txt","r+");

    // check if open succeeded
    if (fp == NULL) return NULL;   

    // Unclear why codes read only 1 char
    // fscanf(fp, "%c", array);
    // Suspect OP wants
    size_t i;
    for(i=0; i<2048; i++){
      if (fscanf(fp, "%c", &array[i]) != 1) break;
    }
    // Could have use fread()

    // More canonical to use type `size_t` than `int` to index arrays.
    // for(int i=0; i<2048; i++){
    for(size_t j=0; j<i; j++){
        printf("%c\n",array[j]);
    }

    // do not forget to put your toys away
    fclose(fp);

    // Simply return `array`
    // return &array;
    return array;
}
Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
2

return a pointer to that array

No, you don't have to do that.

You can write your prototype as void layer4(char array[]) and modify the array directly - the change you made to array inside layer4 will retain after that function exits.

artm
  • 17,291
  • 6
  • 38
  • 54
2

array is already pointer to char. So you need only to return array, not &array.

See this post Arrays decaying into pointers.

Community
  • 1
  • 1
Aleksandar Makragić
  • 1,957
  • 17
  • 32
0

array is already a pointer. So just do: return array;

odin19
  • 147
  • 1
  • 8
0

Your method should take the array as an array or as a pointer, as it is a pointer, then you can just return it as itself. try here

char* layer4(char array[])
{
    ..

    return array;
}
Khaled.K
  • 5,828
  • 1
  • 33
  • 51