1

After using malloc, name gets printed but after allocating memory and typing in a string, puts doesn't print the string at all, neither does printf...why is this?

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


int main()
{
    char *name;
    int size;
    printf("enter the size if name below\n");
    scanf("%d", &size);
    name  =(char*) malloc(size * sizeof(char));//since my compiler returns pointr of type    void, you have specify whether (int*) or (char*)
    if (name== NULL)
    printf("memory allocation failed,,,\n");
    printf("%s\n",name);
    printf("enter name below\n");
    scanf("%s", name);
    printf("name is\n%s", name);
    name = (char*)realloc(name, 100*sizeof(char));
    if (name == NULL)
    printf("failed\n");
    gets(name);
    getchar();
    puts(name);
    free(name);
    return 0;
}
Jim Balter
  • 16,163
  • 3
  • 43
  • 66

3 Answers3

0

First things first, malloc/realloc do not return void, they return void* which is perfectly capable of being implicitly cast to any other pointer type. It's a bad idea to do so explicitly in C since it can hide certain subtle errors.

In addition, sizeof(char) is always one, you do not need to multiply by it.

Thirdly, using gets is a very bad idea since there's no way to protect against buffer overflow. There are much better ways to do user input.

As to the specific problem, I suspect it's most likely still sitting around at the getchar. gets will get a line from the user (including the newline character) but, unless you enter another character (probably a full line if it's using line-based I/O), it will seem to hang. Check this by simply hitting ENTER again after you've entered the name.

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • To precise paxdiablo comment: Chars can be represented **internally** with **any** number of bits (ie, not always a multiple of 8!). But he's true: sizeof(char)==1 : "It is generally not safe for a programmer to presume to know the size of any datatype. For example, even though most implementations of C and C++ on 32-bit systems define type int to be 4 bytes, the size of an int could change when code is ported to a different system, breaking the code. **The exception to this is the char type, whose size is always 1 in any standards-compliant C implementation**." en.wikipedia.org/wiki/Sizeof – Olivier Dulac Jan 02 '14 at 10:09
  • important precision from the same wikipedia: "sizeof is used to calculate the size of any datatype, measured in the number of bytes required to represent the type. **A byte in this context** is the same as an unsigned char, **and may be larger than 8 bits**, although that is uncommon.". Ie, sizeof() is not in bytes ! [although on most system it will be] – Olivier Dulac Jan 02 '14 at 10:10
0

Try either fgets() or scanf() at the place of gets().It will work

Rikesh
  • 26,156
  • 14
  • 79
  • 87
user2760375
  • 2,238
  • 2
  • 22
  • 29
0

The program that you have posted causes undefined behavior. This is because of

printf("%s\n",name);

There is nothing in the variable name and you are trying to print the value in the allocated memory. First you need to assign some value to name before printing it.