#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?
In this code:
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 thatstrlen
returns the length of the string, you will also need the space for the terminating character ('\0'
). You should createreverse
like this:and don't forget to assign
'\0'
to the last character ofreverse
. Also don't forget that the caller of this function becomes responsible for deallocating the memory that has been allocated bymalloc
, i.e. the caller should callfree
on the returned pointer.char reverse[strlen(s)];
is on the stack. After the function complete it is now invalid, but you are returning its address.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 yourmain()
. 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)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.
Instead of
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
fromrev
, which is a local variable.Rather allocate the memory dynamically: