Hamza Mohamed Hamza
Hamza Mohamed Hamza

Reputation: 67

Using scanf with array of pointers

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 ?

Upvotes: 0

Views: 4180

Answers (6)

Raghu Srikanth Reddy
Raghu Srikanth Reddy

Reputation: 2711

char *username[30]

This is an array of pointers to characters..

go for char username[30]

Upvotes: 0

nagaradderKantesh
nagaradderKantesh

Reputation: 1690

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.

Upvotes: 1

Mats Petersson
Mats Petersson

Reputation: 129314

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].

Upvotes: 1

AndersK
AndersK

Reputation: 36082

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]".

Upvotes: 1

Jeyaram
Jeyaram

Reputation: 9474

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.

Upvotes: 1

kmkaplan
kmkaplan

Reputation: 18960

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];

Upvotes: 2

Related Questions