pointers in scanf and printf 
Author Message
 pointers in scanf and printf

i'm using the newest version of the djgpp compiler (gcc ported to windows).
the problem is this...when i do

int main(void)
{
char *name;
scanf("%s",name);
printf("%s\n",name);

Quote:
}

it doesn't work.  it keeps giving me a gpf, but,

int main(void)
{
char *name;
scanf("%s",&name);
printf("%s\n",&name);

Quote:
}

does work.  when &name is used, isn't the address of the pointer being
passed?  it seems to me that we would just want to pass the pointer, the the
functions can just loop through and increment the pointer to access each
element.  even more baffeling (to me anyway) is that the second version only
works if 7 or fewer characters used.  when you try it with 8 characters, it
gives me a gpf.  am i overlooking something?  is this just a stupid mistake?
please help!!! thanks.


Tue, 12 Mar 2002 03:00:00 GMT  
 pointers in scanf and printf


Quote:
> i'm using the newest version of the djgpp compiler (gcc ported to
windows).
> the problem is this...when i do

> int main(void)
> {
> char *name;

This defines a pointer to char. It doesn't define where that pointer is
pointing, and it doesn't allocate any memory.

Quote:
> scanf("%s",name);

So this statement overwrites some random lump of memory. Bad news.

Quote:
> printf("%s\n",name);
> }

> it doesn't work.  it keeps giving me a gpf, but,

> int main(void)
> {
> char *name;
> scanf("%s",&name);
> printf("%s\n",&name);
> }

> does work.

Purest fluke. The code is no more correct than in the first program.
Slightly less correct, if anything.

  when &name is used, isn't the address of the pointer being

Quote:
> passed?

Yes.

Quote:
>   it seems to me that we would just want to pass the pointer,

Yes.

Quote:
>  the the
> functions can just loop through and increment the pointer to access each
> element.

Yes.

Quote:
>  even more baffeling (to me anyway) is that the second version only
> works if 7 or fewer characters used.  when you try it with 8 characters,
it
> gives me a gpf.  am i overlooking something?  is this just a stupid

mistake?

Yes. :-)

Quote:
> please help!!! thanks.

Okay. Reserve some space for your string. Here's an example that uses a
more robust input function as well as correctly using memory:

#include <stdio.h>

int main(void)
{
  char name[64]; /* Most people's names are shorter than 64. */
  printf("Please type in your name: ");
  fflush(stdout);

  if(fgets(name, sizeof name, stdin) != NULL)
  {
    printf("You typed [%s]\n", name);
  }
  else
  {
    printf("The man with no name? I've always wanted to meet you.\n");
  }
  return 0;

Quote:
}

HTH

--
Richard Heathfield

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.



Tue, 12 Mar 2002 03:00:00 GMT  
 pointers in scanf and printf
I assume gpf is "general protection fault"(??) and occurs if there's some
memory usage violation.
That's obvious because  you are using char* name without allocating
an memory space for the variable name.
As a test try using this line
name = malloc( 100 * sizeof(char));
after you declare name.

Note: this is just for testing. YOU have to decide what the size of the buffer
would
be.
NOTE2: As others are bound to point out - scanf is evil...

Cheers
Sreenivas

Quote:
> i'm using the newest version of the djgpp compiler (gcc ported to windows).
> the problem is this...when i do

> int main(void)
> {
> char *name;
> scanf("%s",name);
> printf("%s\n",name);
> }

> it doesn't work.  it keeps giving me a gpf, but,

> int main(void)
> {
> char *name;
> scanf("%s",&name);
> printf("%s\n",&name);
> }



Tue, 12 Mar 2002 03:00:00 GMT  
 pointers in scanf and printf

Quote:

> i'm using the newest version of the djgpp compiler (gcc ported to
> windows).
> the problem is this...when i do

> int main(void)
> {
> char *name;
> scanf("%s",name);
> printf("%s\n",name);
> }

> it doesn't work.  it keeps giving me a gpf, ...

That's because name is not initialized.  It's a pointer that points
nowhere, and then you try to print what it points to (as a string) and
read in what it points to (as a string).  Read up on pointers and
strings in your C book.

Quote:
> ... but,

> int main(void)
> {
> char *name;
> scanf("%s",&name);
> printf("%s\n",&name);
> }

> does work.

It's a coincidence that the second just doesn't happen to crash.  It's
also engaging in undefined behavior.

--

 Alcyone Systems | irc maxxon (efnet) | web http://www.alcyone.com/max/
    San Jose, CA | languages en, eo | icbm 37 20 07 N 121 53 38 W
             USA | Fri 1999 Sep 24 (48%/949) | &tSftDotIotE
 __
/  \ Love's the only reason why we all keep living
\__/ Oleta Adams



Tue, 12 Mar 2002 03:00:00 GMT  
 pointers in scanf and printf
On Fri, 24 Sep 1999 14:33:05 -0400, Sreenivas Talasila

Quote:

>I assume gpf is "general protection fault"(??) and occurs if there's some
>memory usage violation.
>That's obvious because  you are using char* name without allocating
>an memory space for the variable name.
>As a test try using this line
>name = malloc( 100 * sizeof(char));
>after you declare name.

>Note: this is just for testing. YOU have to decide what the size of the buffer
>would
>be.
>NOTE2: As others are bound to point out - scanf is evil...

So is malloc if you don't test for failure.
if(name == NULL)
{
        printf("out of memory\n");
        exit(EXIT_FAILURE);
Quote:
}

/* after your finished with name */
free(name);

Regards,
Greg Martin.



Tue, 12 Mar 2002 03:00:00 GMT  
 pointers in scanf and printf


Quote:


>> i'm using the newest version of the djgpp compiler (gcc ported to
>windows).
>> the problem is this...when i do

>> int main(void)
>> {
>> char *name;

>This defines a pointer to char. It doesn't define where that pointer is
>pointing, and it doesn't allocate any memory.

It allocates memory for the pointer itself but, say you say, nothing for
it to point at.

Quote:
>> scanf("%s",name);

>So this statement overwrites some random lump of memory. Bad news.

As the most basic level it is reading the value of name which at this
point is an uninitialised variable. Reading the value of an uninitialised
variable is always an error, whether it be a pointer or not.

Quote:
>> printf("%s\n",name);
>> }

>> it doesn't work.  it keeps giving me a gpf, but,

>> int main(void)
>> {
>> char *name;
>> scanf("%s",&name);
>> printf("%s\n",&name);
>> }

>> does work.

>Purest fluke. The code is no more correct than in the first program.
>Slightly less correct, if anything.

Well, the only things that *might* be viewed as less correct than
undefined behaviour are syntax errors and constraint violations and
this is neither of those. There is more chance of this working than
the first version on platforms that use the same representation for
all data pointers and where the input string is small: at least
the address sof a valid object is being passed and the string
is written into that.

- Show quoted text -

Quote:
>  when &name is used, isn't the address of the pointer being
>> passed?

>Yes.

>>   it seems to me that we would just want to pass the pointer,

>Yes.

>>  the the
>> functions can just loop through and increment the pointer to access each
>> element.

>Yes.

>>  even more baffeling (to me anyway) is that the second version only
>> works if 7 or fewer characters used.  when you try it with 8 characters,
>it
>> gives me a gpf.  am i overlooking something?  is this just a stupid
>mistake?

>Yes. :-)

>> please help!!! thanks.

>Okay. Reserve some space for your string. Here's an example that uses a
>more robust input function as well as correctly using memory:

>#include <stdio.h>

>int main(void)
>{
>  char name[64]; /* Most people's names are shorter than 64. */
>  printf("Please type in your name: ");
>  fflush(stdout);

>  if(fgets(name, sizeof name, stdin) != NULL)
>  {
>    printf("You typed [%s]\n", name);

This will produce odd output for "short" names since name will
contain a new-line character.

Quote:
>  }
>  else
>  {
>    printf("The man with no name? I've always wanted to meet you.\n");
>  }
>  return 0;
>}

--
-----------------------------------------


-----------------------------------------


Wed, 13 Mar 2002 03:00:00 GMT  
 pointers in scanf and printf

Quote:

> How does Lawrence /do/ this? I sometimes wonder whether we should just
> forward every question to Lawrence rather than bothering to have a go
> ourselves. :-)

I suspect that Lawrence isn't an actual person.  In fact, he's a whole
bunch of people who work together to produce answers and check them
them over for correctness.  The results are edited by a single person
to give consistent style, then sent through for proofreading again and
finally posted.  The occasional typo is inserted automatically by the
posting software just to throw us off and make us believe that "he"'s
fallible like the rest of us.
--
"I should killfile you where you stand, worthless human." --Kaz


Thu, 14 Mar 2002 03:00:00 GMT  
 pointers in scanf and printf


Quote:

>> How does Lawrence /do/ this? I sometimes wonder whether we should just
>> forward every question to Lawrence rather than bothering to have a go
>> ourselves. :-)

>I suspect that Lawrence isn't an actual person.  In fact, he's a whole
>bunch of people who work together to produce answers and check them
>them over for correctness.

Looking for things like repeated words you mean?

John
--
John Winters.  Wallingford, Oxon, England.

The Linux Emporium - a source for Linux CDs in the UK
See http://www.linuxemporium.co.uk/



Fri, 15 Mar 2002 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. questions about sprintf, sscanf, printf, scanf, fscanf, fprintf!

2. Simple scanf/printf question

3. skipped printf/scanf ???

4. tiny question on format of scanf and printf

5. Examples of format strings for scanf/printf ?

6. scanf/printf conversions

7. Beginner question - printf and scanf

8. printf() and scanf()

9. casting printf/scanf

10. Q:printf() and scanf()....

11. printf and scanf in IBMC on IBM 3090

12. printf/scanf trouble on SPARCstation

 

 
Powered by phpBB® Forum Software