#include <stdio.h>
int main()
{
char *Ptr;
scanf( "%s", &Ptr );
printf( "%s", &Ptr );
return 0;
}
Why *Ptr can only take only the first 3 characters from the input? When you put more than 3 characters it doesn't work. Why?
#include <stdio.h>
int main()
{
char *Ptr;
scanf( "%s", &Ptr );
printf( "%s", &Ptr );
return 0;
}
Why *Ptr can only take only the first 3 characters from the input? When you put more than 3 characters it doesn't work. Why?
>Why *Ptr can only take only the first 3 characters from the input?
Because you're calling scanf/printf incorrectly with an uninitialized pointer. This oozes undefined behavior.
This is a classic and common trap. All pointers must have memory allocated to them either directly by anonymous allocation (malloc/calloc/realloc) or by assignment to named memory:
>scanf( "%s", &Ptr );Code:/* Anonymous allocation */ char *Ptr = malloc ( BUFSIZ ); /* Assignment to named memory */ char buff[BUFSIZ]; char *Ptr = buff;
scanf expects a char*, if your variable is already a pointer, you don't need the address-of operator, otherwise you do.
>printf( "%s", &Ptr );
printf's %s flag expects a char*, you're passing a char**. printf is not scanf, remove the address-of operator.
My best code is written with the delete key.
Thanks guys.
Bad what exactly is malloc, and BUFSIZ?
This works:
/* Assignment to named memory */
char buff[BUFSIZ];
char *Ptr = buff;
but this doesn't work:
/* Anonymous allocation */
char *Ptr = malloc ( BUFSIZ );
why?
I have another problem:
I can't make an array of strings using pointers, without knowing its elements in advance.
I type:
char buff[10];
char *Ptr[20] = { buff };
but it doesn't work.
What's the right cote?
Help me pls.
>but this doesn't work
'Doesn't work' is a very vague problem description, can you specify how it doesn't work as opposed to how you expected it to? Perhaps post the code you're using so that we can play with it as well?
>I can't make an array of strings using pointers, without knowing its elements in advance.
You're stuck then. At some point you have to specify how many characters are in the string so that you can allocate the appropriate amount of memory. For just a simple array of strings that won't change you can do something like this:
However, you can't modify the memory that the pointers refer to because string literals are not modifiable. There are tricky ways to create variable length arrays of variable length strings, but they can get very complex and shouldn't be used unless absolutely necessary. The simplest solution is simply to bite the bullet and create a two dimensional array.Code:char *Ptr[] = { "First", "Second", "Third", };
My best code is written with the delete key.
I type:
char buff[10];
char *Ptr[20] = { buff };
but it doesn't work.
What's the right cote?
>char *Ptr[20] = { buff };
This tries to initialize a pointer variable with the address of a local object. This isn't standard behavior, so it won't work unless your compiler supports an extension. The closest standard version would be
This assigns the address of buff to the first element of Ptr and leaves the rest of them uninitialized.Code:char *Ptr[20]; Ptr[0] = buff;
My best code is written with the delete key.
Stop spamming the board. We've read your post. You haven't read the replies. Do not just say "it doesn't work". That tells me nothing.Originally posted by AProg
I type:
char buff[10];
char *Ptr[20] = { buff };
but it doesn't work.
What's the right cote?
If it doesn't compile, say it doesn't. Then say what it exactly does do. (IE: Error messages.)
If it doesn't do what you expect, state what you expect it to do, and tell us what it does or doesn't do.
Be helpful if you wan't help, otherwise, I'll just $$$$$ you out.
Quzah.
Hope is the first step on the road to disappointment.
Look:
The problem is that i can't declare the arrey of pointers. During the execution of the programm, each pointer will get a string.
Let's suppose that we will get 10 strings consisting of 20 characters.
What's the code of the declearetion?
Thanks.
But how can i do this using pointers?
Thanks again.
But can u explain me what exaxtly malloc does, and why i have to use it?
Before answering malloc:
I follow your advice and i type:
int main()
{
char names[10][20];
int i, n;
scanf( "%d", &n );
for( i = 0; i<= n-1; i++ )
scanf( "%s", names[i] );
for( i = 0; i<= n-1; i++ )
printf( "%s\n", names[i] );
return 0;
}
It works properly, but prossesor fails and tells me : programm stopped( not source available )
why?
It works properly but it fails? Do you mean that it compiles fine, but gives you a runtime error?
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
It prints the right results, but compiler fail sand program terminates( no source available ).
Why?