0
char* username[30];
memset(username,0x00,30);
scanf("%s",&username);

would this make pointers point to random place in memory or it's safe to use ?

2
  • What are you trying to do? Commented Jan 9, 2013 at 9:00
  • 1
    Syntactic problems aside, scanf() is very poorly suited to do anything other than reading in data that was previously written by your own program. Probably not even that, because of the very poor error handling provided by scanf(). Commented Jan 9, 2013 at 9:05

6 Answers 6

2

char *username[30] is an array of pointers, not characters. So your code is very wrong (as in not safe). To get an array of characters you need:

char username[30];
Sign up to request clarification or add additional context in comments.

Comments

1
char* username[30]; //is array of char pointers.

//Allocate memory for these pointers using calloc(). so no need of memset().  

memset(username,0x00,30);//can be removed.

scanf("%s",&username);//It should be scanf("%s",username[i]);

@perreal, Sample added.

#define SIZE 100 //100 chars...
char* username[30]; 
int i;

for(i = 0; i < 30; i++)
{
   username[i] = calloc(SIZE, sizeof(char)); //Add Fail checks if needed.
   scanf("%s",username[i]);
}

so with the above code, you can get 30 strings. If you need only one string with 30 char then

char username[30];
memset(username,0x00,30);
scanf("%s",username);

is enough.

Comments

1

with

memset(username,0x00,30);  

you are initializing the first 30 bytes of your array of pointers and not the whole array

memset(username,0, sizeof(username));

would set everything to 0 although a simple loop is clearer for the reader (IMHO)

for (int i = 0; i < 30; username[i++] = NULL) {;}

don't do this:

scanf("%s",&username);

scanf doesn't magically allocate anything -- "username" is an array of pointers and are NULL pointers, how should scanf know how to allocate memory etc? Instead do a loop, let user enter a string, allocate memory for that string (+1), copy the string to the allocated memory and assign it to "username[i]".

Comments

1

What you PROBABLY want is:

int i;
char* username[30];
for(i = 0; i < 30; i++)
{
     username[i] = calloc(100, sizeof(char));  // or whatever size your string is.
     scanf("%s",username[i]);
}
... Code using usernames ...
for(i = 0; i < 30; i++)
{
    free(username[i]);
}

But personally, I'd probably go for:

int i;
char username[30][100];
for(i = 0; i < 30; i++)
{
     scanf("%s",username[i]);
}

Saves on having to free the pointers later.

That will read 30 strings into your username array.

If you want to just read one username:

char username[30] = {0};    // Same as memset, but shorter to write!
scanf("%s", username);

Although as others have suggested, scanf() is't the best function to read "user generated input" - it's fine for data that your program has already "checked" (that is, it contains no "funny stuff", fits in the length provided, etc) and written to a file [using fscanf()]. For user input, use fgets() to read a line of text, and then work through it in whatever way is suitable to get the actual data out of the string.

For example, if some username has more than 100 characters [or thirty in the last example], the string will overflow, and nothing good will ever come from that [and in really bad cases, you won't notice until MUCH later, which makes it hard to debug - if you are lucky, it crashes immediately].

2 Comments

Very sound advice all-around, especially getting rid of the malloc().
I have a mild alergy to "malloc", so I try to avoid it if possible... ;)
1
char* username[30];
memset(username,0x00,30);
scanf("%s",&username);

the above your code will get crash , because you are trying to input into pointer for which memory is not allocated. so first you allocate memory for the pointers and then you read into that memory location.

Comments

0
char *username[30]

This is an array of pointers to characters..

go for char username[30]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.