0

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?

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • Note that there's a right parenthesis (`)`) missing in `malloc(sizeof(char*MAX);`, and usually that kind of thing confuses compilers and causes error messages that have nothing to do with the error. – Brendan Sep 02 '19 at 01:40
  • Welcome to SO! Just to elaborate on the answers, when coding, I strongly recommend adding only one line at a time, then executing the code. This way, you won't accumulate 6-7 mistakes and get overwhelmed with confusing problems, but instead will detect each bug as it arises and can be easily fixed. – ggorlen Sep 02 '19 at 01:58

3 Answers3

1

Well, there are several errors:

  1. malloc(sizeof(char*MAX) should be malloc(sizeof(char) * MAX).
  2. gets() should never be used, use fgets(name, MAX, stdin).
  3. char space = " "; is wrong, it should be char space = ' ';, since " " is a string, not a character.
  4. char surname = *getSurname(&name) is also wrong in two ways: first, you don't need to use *, you have to declare surname as char *, not char; second, you don't need to use &name either. The correct call would be char *surname = getSurname(name);.
  5. You also have to check for '\0' when scanning through name, to avoid stepping beyond the end of the string.
  6. You stop at the space in the while, you should go one char further, adding a name++ before returning in case the space is found.
  7. You should check the return value of malloc(), and if returns NULL, exit the program.
  8. It's good practice to free(name) before returning from main, though it's not strictly needed.

Correct code would be:

#include <stdio.h>
#include <stdlib.h>

#define MAX 128

char *getSurname(char *name)
{
    while (*name != '\0' && *name != ' ')
        name++;

    if (*name == ' ')
        name++;

    return name;
}

int main(void)
{
    char *name = malloc(sizeof(char) * MAX);
    if (name == NULL) {
        perror("malloc() failed");
        return 1;
    }

    printf("Insert your full name: ");
    fgets(name, MAX, stdin);

    char *surname = getSurname(name);
    printf("Hello %s!\n", surname);

    free(name);
    return 0;
}

Also, as a sidenote, fgets() stops at the first newline, so you should remove the newline from the name before processing it, otherwise you would get an output like this:

Hello Relbeits
!

You could do this after calling fgets():

char *nl = strchr(name, '\n');
if (nl != NULL)
    *nl = '\0';
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
0

in getSurname function

char space = ' ';

" " is string, not character. need variable type char*

in main function

char *name = (char*)malloc(sizeof(char) * MAX);

Parentheses Pair Mismatch

sizeof parameter problem

char *surname = getSurname(name);

getSurname function is not pointer function.

getSurname function return type is character pointer.

parameter type is char* of getSurname function parameter &name is type char**

HATENA
  • 33
  • 5
  • 1
    [No need to cast the return value of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – ggorlen Sep 02 '19 at 01:52
0

There are several bugs. I've annotated and fixed them below. I think it'll be easier to see this way.

#include <stdio.h>
#include <stdlib.h>
#if 1
#include <string.h>
#endif

#define MAX 128

char *
getSurname(char *name)
{
// NOTE/BUG: initialization of a _scalar_ must use single quotes
#if 0
    char space = " ";
#else
    char space = ' ';
#endif

// NOTE/BUG: this has no check for end of string
// NOTE/BUG: this returns a pointer to the _space_ and _not_ the first char
// of the last name
#if 0
    while (*name != space) {
        name++;
    }
#else
    for (;  *name != 0;  ++name) {
        if (*name == space) {
            ++name;
            break;
        }
    }
#endif

    return name;
}

int
main(void)
{
// NOTE/BUG: this is a syntax error [not enough right paren]
#if 0
    char *name = malloc(sizeof(char *MAX);
#else
    char *name = malloc(sizeof(*name) * MAX);
#endif

// NOTE/BUG: do _not_ use 'gets'
    printf("Insert your full name: ");
#if 0
    gets(name);
#else
    fgets(name,MAX,stdin);
    char *cp = strchr(name,'\n');
    if (cp != NULL)
        *cp = 0;
#endif

// NOTE/BUG: you want the address value of what name _points to_ and _not_
// the address of name itself
// NOTE/BUG: you want to return and save a _pointer_ and _not_ a scalar
#if 0
    char surname = *getSurname(&name);
#else
    char *surname = getSurname(name);
#endif
    printf("Hello %s!\n", surname);

    return 0;
}
Craig Estey
  • 30,627
  • 4
  • 24
  • 48