I'm trying to code a program that allows the user to input his full name and then the program output only his full surname. To do so, I made a pointer, with the malloc sized to 128 chars, that is supposed to store the name later.
I've tryed to run the program with CLion, Netbeans, Code Blocks and now at this site, but they are pointing some errors... so the program can't run.
The problem is that the program is not even compiling, saying that there's some erros... these are the build messages:
|=== Build: Debug in exerciseSixteen (compiler: GNU GCC Compiler) ===|
| LINE | MESSAGE
| |In function 'getSurname':
| 08 |warning: initialization makes integer from pointer without a cast [-Wint-conversion]
| |In function 'main':
| 04 |error: expected ')' before numeric constant
| 17 |note: in expansion of macro 'MAX'
| 21 |warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat=]|
|=== Build failed: 1 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|
I even uninstalled the MinGW compiler and the MSyS and re-installed the CodeBlocks that also installs MinGW togheter, but now I know that my problem is not related with the compiler or the IDE... it's probably the code, that by the way, can be seen below:
#include <stdio.h>
#include <stdlib.h>
#define MAX 128
char *getSurname(char *name)
{
char space = " ";
while (*name!=space) {
name++;
}
return name;
}
int main()
{
char *name = malloc(sizeof(char*MAX));
printf("Insert your full name: ");
gets(name);
char surname = *getSurname(name);
printf("Hello %s!\n", surname);
return 0;
}
I can't see any errors on this code, I tough it was all ok... what am I missing? What I can do to see it working?