I am trying to allocate memory for three string and print it to screen.but it isnt working .can anyone explain what's wrong here

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    char **name;
    int i;
    name=(char **)malloc(3 * sizeof(char *));
    while(i<3){
               name[i]=(char *)malloc(20 * sizeof(char));
               }
              while(i<3){
                         scanf("%s",name[i]);
                         }   
               while(i<3){
                        printf("%s",name[i]);
                         }         

               getch();
}

Recommended Answers

All 9 Replies

You failed to initialize and set the value of variable i. It just contains some random value. A for loop would be better than the while loop.

ok i did.but no result. whenever i try to execute,it fails.cant even input the names and pc hangs.

you need something like this:

for(i = 0; i < 3; i++)
{
   scanf("%s", names[i]);
   printf("You entered '%s'\n", names[i]);
}

But beware of scanf() because it will let you enter more characters than the buffer can hold. Try entering some text of 30 characters and see what happens. You can tell scanf() to limit the number of characters like this: "%20s" where the value 20 is the size of the buffer.

Thnx,that works.To see what happens i replaced scanf with gets and fgets .fgets includes the terminating null character at the end in the output,but gets doesn't.Then if i inter a 20 character string and press enter that makes it 21 character long.It should be overflowed.But it's working fine.Can't understand why???

.fgets includes the terminating null character at the end in the output,but gets doesn't

I think you are confused about the difference between fgets() and get(). fgets() puts (if there is room) the '\n' (Enter key) at the end of the string. Both fgets() and gets() att the '\0' null terminator at the end.

Just because the program didn't crash doesn't mean its "working fine". If you used gets() and entered 20 characters, where do you think gets() will save the extra characters? Answer it will probably overwrite some other variable that was declared nearby, or just scribble the extras somewhere in memory. Instead of entering 20 characters enter 50 characters (just type gibberish) and see what heppens.

yes there is some confusion.Actually the question was about the output.when i use gets -
my input was
1.woody
2.woodpecker
3.asdfghjklpoiuytrewqa (20 char)
output -> (woody)(woodpecker)(asdfghjklpoiuytrewqa) in the same line and every character prints.Cant find where the null character goes!!
but if i use fgets
fgets(name[i],20,stdin);
with same input,the output was
-woody
-woodpecker
-asdfghjklpoiuytrewq
in seperate lines and the last a is missing.why it is missing?

The '\0' is overwritten for the reassons I previously mention, if you want to enter 20 characters then you have to make the length of the string 21 so that there is enough room for the '\0'.

i understand what u said.i am new to c and trying to understand things.i think fgets include a newline character '\n' at the end of every output what gets doesn't.

fgets() puts '\n' at the end only if there is room for it. If the buffer is not large enough to hold all the characters then fgets() will put however many characters it can in the buffer and leave the rest in the keyboard buffer. That makes a good way to see if the keyboard buffer is empty or not, and if not then call fgets() again to geet the remaining characters.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.