array return type

2020-05-11 09:46发布

#include<stdio.h>
#include<string.h>
#define MAX 30

void push(char );


char stack[MAX];
int tos=0;

int main(){
    char str[]="Arijit Saha";
    char *final;
    final=(char *)rev(str);
    printf("%s",final);
    rev(str);
    getch();
    return 1;
}

char* rev(char s[]){
    int i;
    for(i=0;i<strlen(s);i++)
        push(s[i]);
    char reverse[strlen(s)];

    for(i=0;i<strlen(s);i++)
        reverse[i]=pop();

    return reverse;

}

void push(char c){
    stack[tos]=c;
    tos++;
}
int pop(){
    tos--;
    return stack[tos+1];

}

These are the error messages..

Compiler: Default compiler
    Executing  gcc.exe...
    gcc.exe "C:\TC\BIN\stringrevusingstack.c" -o "C:\TC\BIN\stringrevusingstack.exe"   -g3  -I"C:\TC\INCLUDE"  -I"C:\Dev-Cpp\include"  -I"C:\Program Files\ImageMagick-6.7.4-Q16\include"   -L"C:\Dev-Cpp\lib" -L"C:\TC\LIB" -L"C:\Program Files\ImageMagick-6.7.4-Q16\lib" -g3 
    C:\TC\BIN\stringrevusingstack.c:20: error: conflicting types for 'rev'
    C:\TC\BIN\stringrevusingstack.c:14: error: previous implicit declaration of 'rev' was here

    C:\TC\BIN\stringrevusingstack.c: In function `rev':
    C:\TC\BIN\stringrevusingstack.c:28: warning: function returns address of local variable

    Execution terminated

What's going wrong?

标签: c
5条回答
够拽才男人
2楼-- · 2020-05-11 10:07

In this code:

char* rev(char s[]) {
    char reverse[strlen(s)];
    ...
    return reverse;
}

reverse is an temporary array with automatic storage duration that is being deallocated once the execution leaves the scope of this function. You return a pointer that becomes a dangling pointer.
Trying access the memory that this pointer points to produces undefined behavior.

Apart from the fact that you should allocate it dynamically by using malloc, note that strlen returns the length of the string, you will also need the space for the terminating character ('\0'). You should create reverse like this:

char* reverse = malloc(strlen(s) + 1);

and don't forget to assign '\0' to the last character of reverse. Also don't forget that the caller of this function becomes responsible for deallocating the memory that has been allocated by malloc, i.e. the caller should call free on the returned pointer.

查看更多
不美不萌又怎样
3楼-- · 2020-05-11 10:09

char reverse[strlen(s)]; is on the stack. After the function complete it is now invalid, but you are returning its address.

查看更多
够拽才男人
4楼-- · 2020-05-11 10:14

c:14: error: previous implicit declaration of 'rev' was here

Your error is because you didn't generate a prototype for rev() before you used it in your main(). Either move your function above main or add a prototype.

c28: warning: function returns address of local variable

Your warning is because you are trying to return the address of a local variable, you can't do that. Local variables are out of scope when you leave the function, so you need to do something else (such as use a dynamic array via adding malloc()/free() calls)

查看更多
Deceive 欺骗
5楼-- · 2020-05-11 10:17

reverse is a local array. It is destroyed, when the function exits, but you return a pointer to its contents. Ideally, you should return the array by having a parameter that the function will load data into, i.e.

void MyFuncReturnsArray(int* myArr, int n)
{
   for(int i = 0; i < n; ++i)
      myArr[i] = i;
}

Instead of

int* MyFuncReturnsArray()
{
     int myArr[10];
     for(int i = 0; i < 10; ++i)
        myArr[i] = i;
     return myArr

}
查看更多
爷的心禁止访问
6楼-- · 2020-05-11 10:22

A variable which has automatic storage duration doesn't exist anymore in the calling function. Accessing to it leads to an undefined behavior (anything can happen). Here you are returning reverse from rev, which is a local variable.

Rather allocate the memory dynamically:

int *reverse = malloc(strlen(s)); /* + 1 for '\0' character ? */
查看更多
登录 后发表回答