gettng error in c program,very simple

Asked by raushan kumar

#include <stdio.h>
void main()
{
char name[50], *p;
printf("enter the name\n");
gets(name);
p=&name[50];
printf("the name is \n");
while(*p!=NULL)
{printf("%c",*p);
p++;
}
}
}

output:
1.c: In function ‘main’:
1.c:9: warning: comparison between pointer and integer
1.c: At top level:
1.c:14: error: expected identifier or ‘(’ before ‘}’ token
raushan@raushan:~/Documents/test.c$

i am not understanding where's the mistake in main()
thank you

Question information

Language:
English Edit question
Status:
Answered
For:
Ubuntu gcc-defaults Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Eliah Kagan (degeneracypressure) said :
#1

The warning is because, in C, NULL is a macro for (void*)0, whereas the zero that terminates a C-style string is '\0' (i.e. (char)0, but don't call it that). The void* and char data types should generally not be compared with one another. So you should change your code to say:

while (*p != '\0')

The error is because you have an extra } at the end of the program. Remove that, and you'll be fine.

I understand that Launchpad compacts whitespace, but if you're *actually* not indenting, then you should. If you want to post source code in a way where the original whitespace can be seen, you can post it at http://pastebin.ubuntu.com/ (select C in the Syntax drop-down menu), and then post a link to it here.

Your text editor should tell you what line number your cursor is on, and be able to jump to a specified line. It should also have an option, that you can enable if you wish, for showing the number of each line (to the left of the line). By knowing which line the compiler is reporting an error in, you'll often be able to figure out the cause of the problem.

Revision history for this message
Eliah Kagan (degeneracypressure) said :
#2

Also, while the compiler didn't catch this...you don't want to assign p the address of the (out-of-bounds) 51st element of the array called name. You want to assign it the address of the first element of the array called name. So you should say "p = name" (or "p = &name[0]", if you insist on a more cumbersome notation) instead of "p=&name[50]".

Revision history for this message
Eliah Kagan (degeneracypressure) said :
#3

Something else you may find valuable:

If you consider your loop, you'll see that you are initializing a variable (p), which is checked in every iteration of the loop to see if the loop should proceed, and which is incremented at the end of each iteration of the loop. That suggests that a for-loop would be preferable to a while-loop. A for-loop to perform the same task would look like this:

for (p = name; *p != '\0'; p++) printf("%c", *p);

(In that case you would remove the line before the second printf call, which initializes p, since p is initialized in the for loop itself.)

Since you're printing one character at a time, you may wish to use putchar instead of printf, which is lighter-weight and has simpler syntax:

for (p = name; *p != '\0'; p++) putchar(*p);

After the loop (whether or not you make it a for-loop), you're probably going to want to have a carriage return and line break (i.e. you want the cursor to be at the beginning of the next line). You can do this with:

printf("\n");

Or, better:

putchar('\n');

I'm assuming that you're writing this program as an exercise with loops, since in general if your goal is to print a C-style string verbatim to standard output and have it be terminated with a newline, you'd use:

puts(name);

By the way, as an additional exercise, you may wish to consider what happens if the user presses Ctrl+D when prompted for their name, and modify your program to handle this special case (http://en.wikipedia.org/wiki/End-of-file and http://manpages.ubuntu.com/manpages/maverick/en/man3/gets.3.html may help you with this). As a more advanced exercise, you may wish to modify your program so that no matter how long of a name the user tries to enter, no buffer overflow occurs. Of course, please feel free to ignore these suggestions if they are not of interest to you!

Can you help with this problem?

Provide an answer of your own, or ask raushan kumar for more information if necessary.

To post a message you must log in.