nofearse
asked on
How to print the value of a char* type data in C
i have something like:
unsigned int num = 0;
char* aString = "aaa";
printf("enter data: \n");
scanf("%u%s", &num, &aString);
printf("%u\t%s\n", num, aString);
it doesnt work, gives segmentation fault on gcc
How can i fix it?
unsigned int num = 0;
char* aString = "aaa";
printf("enter data: \n");
scanf("%u%s", &num, &aString);
printf("%u\t%s\n", num, aString);
it doesnt work, gives segmentation fault on gcc
How can i fix it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I think the scanf statement should be
scanf("%u%s", &num, aString);
scanf("%u%s", &num, aString);
ASKER
What if I don't know what size the maximum length of the string is
Does C has INT_MAX or something, just like C++?
Does C has INT_MAX or something, just like C++?
> What if I don't know what size the maximum length of the string is
> Does C has INT_MAX or something, just like C++?
Yes, INT_MAX is in the ANSI C spec. However, it sounds like you are contemplating
something like char buffer[INT_MAX]; which would be a 2 gigabyte buffer - unwise.
Typically, users of scanf() create a "sufficiently large" buffer (in my example, I used
1 a kilobyte buffer, however 2, 4, 8 and 16 kilobyte scratch buffers are not uncommon.
The wise programmer will also use input functions that avoid overflowing the input
buffer, like fgets() or a field width specification for scanf().
> Does C has INT_MAX or something, just like C++?
Yes, INT_MAX is in the ANSI C spec. However, it sounds like you are contemplating
something like char buffer[INT_MAX]; which would be a 2 gigabyte buffer - unwise.
Typically, users of scanf() create a "sufficiently large" buffer (in my example, I used
1 a kilobyte buffer, however 2, 4, 8 and 16 kilobyte scratch buffers are not uncommon.
The wise programmer will also use input functions that avoid overflowing the input
buffer, like fgets() or a field width specification for scanf().
Hi nofearse,
> What if I don't know what size the maximum length of the string is
> Does C has INT_MAX or something, just like C++?
That's a classic security problem: buffer overflow. Never, ever use a function which does not limit the input size. Even if your program isn't security-sensitive, it will be much more stable when you use fgets() instead for your string.
Cheers!
Stefan
> What if I don't know what size the maximum length of the string is
> Does C has INT_MAX or something, just like C++?
That's a classic security problem: buffer overflow. Never, ever use a function which does not limit the input size. Even if your program isn't security-sensitive, it will be much more stable when you use fgets() instead for your string.
Cheers!
Stefan
i think fgets and sscanf combination will work
char * aString, * buffer;
aString = (char *) malloc(MAX_LEN + 2);
do {
if (!fgets(buffer, MAX_INPUT_LEN + 2, stdin))
return 0;
if (sscanf(buffer, "%s", aString) != 1)
continue;
}
while (1);
char * aString, * buffer;
aString = (char *) malloc(MAX_LEN + 2);
do {
if (!fgets(buffer, MAX_INPUT_LEN + 2, stdin))
return 0;
if (sscanf(buffer, "%s", aString) != 1)
continue;
}
while (1);
Hi manojantony,
While are you doing a sscanf() here? strcpy() is faster and more straight-forward.
Kent
While are you doing a sscanf() here? strcpy() is faster and more straight-forward.
Kent
Kent,
Its for input validation..
if (sscanf(buffer, "%s", aString) != 1)
^
if (sscanf(buffer, "%d%d%d", aString) != 3)
^
hope you got the point
-
MA
Its for input validation..
if (sscanf(buffer, "%s", aString) != 1)
^
if (sscanf(buffer, "%d%d%d", aString) != 3)
^
hope you got the point
-
MA
Ok, but in your example I believe that you will always return 1.
Kent
Ye u r right. I agree.. its fine .. for that particular example. :-)
currently occupied by the POINTER to "aaa". There are several things wrong with this:
1) scanf("%s") expects a pointer to char. You are passing a pointer to a pointer to char.
You have made one of two possible common errors:
a) You were trying to have scanf() store the input data into the bytes of memory
currently occupied by "aaa". See items 2 and 3 below.
b) You were expecting scanf() to return a pointer to new or existing memory
that holds the string, modifying the pointer (aString) to point to that new location
(and no longer point at "aaa"). See item 4 below.
2) If the user enters more than 3 characters, the input will overflow the 4 bytes
[or 8 bytes - depending on sizeof(pointer) for your platform] overwriting adjacent data.
3) String constants may be (but are not required to be) stored in read-only memory.
Attempting to write into read-only memory will yield a segmentation fault.
4) scanf("%s") expects a pointer to an array of char large enough to hold the expected
input. You should consider declaring aString as: char aString[1024];
The char array will be mutable and sufficiently large to accept most input.
You can also limit the number of bytes read into aString by specifying a width
to the field read:
scanf("%u%s", &num, &aString);