Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NEWBIE IN DESTRESS 1

Status
Not open for further replies.

VspecGTR3

Technical User
Feb 3, 2003
62
0
0
US
im like the subject im new to c im currently reading C for MeatHeads (eheh ok dumbies) and i followed the book and im using mircle c to write and compile but my first script (not counting hello world) wont work i followed the book and it jsut dont seem to get it why it doesnt work take a gander

#include <stdio.h>

void main()
{
char me[20];

printf(&quot;What is your name?&quot;);
scanf(&quot;%s&quot;,&me);
printf(&quot;Darn glad to meet you, %s!\n&quot;,me);
}
 
Passing the address of an int is different from passing the address of the address of a string. That's too much indirection.

But it looks like we can agree that &quot;me&quot; and &quot;&me&quot; are two different values. Reading into a string expecting to start at the address &quot;me&quot; is different from reading into a string expecting to start at the address &quot;&me&quot;. I'm not sure if it should be compiling or not, but the &quot;&&quot; definitely needs to be removed.
 
You must not expect this: scanf(&quot; %s&quot;, &me);
to be the proper way to assign a string to
the char array: char me[20];.
At best it is redundant and confusing.
Using this idea try this program:

Code:
#include <stdio.h>
#include <stdlib.h>

void printaddy(char *);


int main(void) {
char me[20];


   printf(&quot;Enter your name: &quot;);
   scanf(&quot; %s&quot;, me);
   printf(&quot;My name is %s at %p\n&quot;, me, &me);
   printaddy(me);
   printf(&quot;Enter your name: &quot;);
   scanf(&quot; %s&quot;,&me);
   printf(&quot;My name is %s at %p\n&quot;, me, &me);
   printaddy(me);
 return 0;
}


void printaddy(char *str) {

   while (*str) {
         printf(&quot;%c at %p\n&quot;, *str,&(*str));
         *str++;
   }
}

Now try to pass the array me to printaddy using the
same syntax mqonnet suggests: printaddy(&me);
gcc says:
bo.c: In function `main':
bo.c:14: warning: passing arg 1 of `printaddy' from incompatible pointer type

In other words using this method, you create problems
for your compiler and yourself even if it does work.
Learning correctly the first time is better than trying
to unlearn bad habits.





 
Vspec,
The reason it doesnt wait in the window on DOS is because you dont have a wait after it displays. Which is usually achieved by getch or getchar().

Chipper, i dont think i totally agree with you.
&quot;Reading into a string expecting to start at the address &quot;me&quot; is different from reading into a string expecting to start at the address &quot;&me&quot;. I'm not sure if it should be compiling or not, but the &quot;&&quot; definitely needs to be removed.&quot;

From the explaination of marsd its clear again which i mentioned earlier that using &me will work. Let me again try and explain why.
When you pass in an address to scanf to read something, it does not put the value directly into the address location, but it puts it to the location that address/pointer is pointing to. Say me points at 100 and &me 200. Scanf would get 200, but the value of 200 is a pointer again and which is 100. So, it would eventually start putting the value from 100 on.

marsd. You are right. This is the very appropriate way to learn. But from the beginning my point of view was that THIS WILL WORK. I never mentioned either that this is the right way to do things.
But also to add to your example. You have to bear in mind that printf takes in a variable as argument and pulls out the value associated with the variable. Whereas scanf needs an address location to redirect the stdin.

Cheers.
KK IBM MQSeries Specialist
 
Right..
Sorry for the confusion.

Sometimes saying something will work is
misconstrued as it being an accepted/acceptable
way of doing things.

Sorry to wax pedantic. Too much time on
comp.lang.c I guess ;)
 
I'm completely with mqonnet on this one. Getting the address of a pointer can even be useful at times, if you are going to assign an address to a pointer in a function, for example. //Daniel
 
I don't disagree that taking the address of a pointer is legal and useful. I never did. That wasn't an argument. My exact words were, &quot;Taking the address of an address makes no sense (in this case).&quot;

All I said was that scanf( &quot;%s&quot;, me ) and scanf( &quot;%s&quot;, &me ) have different meanings and shouldn't be treated equivalently. If they are, it's either a compiler &quot;feature&quot; or some scanf magic.

At the point the scanf arguments are being looked at, no type checking is being done. It can't tell the difference between a pointer, a pointer to a pointer, an int, or the first half of a double. The only thing that determines what type of value is put in there is the format string.

If that format string is &quot;%s&quot;, it expects the corresponding argument to be a char*. It doesn't check. If it's an integer, it tries to write into the &quot;address&quot; that integer contains. If it's a char**, it tries to write into the location where the pointer to the string is stored... not the actual string itself (as was probably intended). If scanf or the compiler does some magic to &quot;fix&quot; this, you can't count on that happening everywhere.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top