I'm getting Segmentation Fault from this code, Can anyone tell me what's wrong? I'm getting a Segmentation Fault so I assume I messed up the pointers at some point. It should return and print the greatest number in the string.
It should return number 5.
#include <stdio.h>
#include <string.h>
int* greatest(char* string);
int main()
{
char *string = "This4 is5a 3wonderful2 day";
int *p = 0;
p = greatest(string);
printf("%d\n",*p);
}
int* greatest(char* string)
{
int i = 0;
int *poi = 0;
while (*(string + i) != '\0')
{
if(*(string + i) >= '0' && *(string + i) <= '9')
{
if(*((string + i)-48) > *poi)
{
*poi = *((string + i)-48);
}
}
i++;
}
return poi;
}
This is what I'm getting after executing the code:
" Segmentation fault (core dumped)"