1

Possible Duplicate:
Dynamic String Input - using scanf(“%as”)
strcmp with pointers not working in C

Is the following considered good code? Shouldn't I have used malloc somewhere? I was able to compile this and it worked, but I feel like it shouldn't have.

#include <stdio.h>

int main (void) {

    char *name;

    printf("Whats your name? ");
    scanf("%s", &name);
    printf("\nyour name is %s", &name);

    return 0;
}

What happens if I want to modify name? How would I go about doing so?

Edit: I am really just looking for the most efficient and correct way to do this using pointers. I am assuming malloc is necessary.

Community
  • 1
  • 1
kyle
  • 2,563
  • 6
  • 22
  • 37
  • 6
    That's a very bad code. I suggest you first learn about how memory allocation works in C. You're using an uninitialized pointer. – m0skit0 Nov 06 '12 at 23:41
  • 1. Not a duplicate, I'm another poster. Similar question but not the same. 2. What are your recommendations on how it should be done? I realize that this is wrong, but I can't seem to find a good solution. – kyle Nov 06 '12 at 23:47
  • 1
    Worse than using an uninitialized pointer, you're using an **address** of an uninitialized pointer. Even if you allocated memory and pointed `name` at it, it is still wrong on the `scanf()` and `printf()` calls. – WhozCraig Nov 06 '12 at 23:47
  • You should initialize a string with a defined value, You can use alternatively, gets() to avoid the standard input buffer – Alberto Bonsanto Nov 06 '12 at 23:53
  • @LewsTherin This is not at all related to that question, which is about a glibc feature (%as) that automatically allocates memory. Kyle, whether a question is a duplicate has nothing to do with who asked it. – Jim Balter Nov 07 '12 at 01:48
  • @JimBalter Should've read what was linked.. – Lews Therin Nov 07 '12 at 01:55

2 Answers2

6

name is a pointer, and &name returns the address of the variable name, so the scanf is putting the name you enter into the pointer itself.

For example, if you enter ABC then the pointer will be 0x00434241 (if the CPU is little-endian) or 0x41434200 (if the CPU is big-endian), where 0x41 is the character code for 'A', 0x42 is the character code for 'B', etc.

You should allocate memory into which the entered name can be stored and then pass a pointer to it to scanf.

Here's an example allocating on the stack:

#include <stdio.h>

#define MAX_NAME_LENGTH 256

int main (void) {

    char name[MAX_NAME_LENGTH];

    printf("Whats your name? ");
    scanf("%s", name);
    printf("\nyour name is %s", name);

    return 0;
}
MRAB
  • 20,356
  • 6
  • 40
  • 33
  • This is originally what I was planning on doing, but will defining the array for name, take up space you don't need? – kyle Nov 06 '12 at 23:57
  • @kyle: yes it will, up to 256 bytes of it, depending on how long a string you input. – Fred Foo Nov 07 '12 at 00:08
  • @kyle: You don't know how much memory it'll need for the name, so allocate plenty as an input buffer. You can then dynamically allocate only as much as is actually needed, copy the name into it, and then reuse the buffer for the next input. – MRAB Nov 07 '12 at 00:17
  • If you wanna a smaller memory footprint, use dynamic malloc. If you want your program run faster, use pre-sized array. And you can just use array as pointers. – SwiftMango Nov 07 '12 at 00:38
  • @texasbruce could you give me an example of dynamic malloc? – kyle Nov 07 '12 at 01:15
  • 4
    When you scanf into a fixed size buffer, always give a maximum size to avoid buffer overflow: `scanf("%255s", name);` – Chris Dodd Nov 07 '12 at 01:17
-1

Alternatively You can use gets too, to avoid the Standard Input buffer in cases that you have more than 2 sequential inputs.

#include <stdio.h>

#define LENGTH 256

int main (void) {

   char name[LENGTH];

   printf( "Whats your name? " );
   fgets( name, sizeof( name ), stdin );
   printf( "\nYour name is %s", name );

   return 0;
}
Alberto Bonsanto
  • 17,556
  • 10
  • 64
  • 93