INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

receiving strings from keyboard and storing base address in array of pointers...

receiving strings from keyboard and storing base address in array of pointers...

receiving strings from keyboard and storing base address in array of pointers...

(OP)
void main()
{
char *names[6];
int i;
for(i=0;i<6;i++)
{
scanf("%s",names[i]);
}
getch();
}

i am writing this code and compiller is not giving any error.i am using dev c++.why error?

RE: receiving strings from keyboard and storing base address in array of pointers...

You haven't allocated any space for the names. Say you allow up to 99 characters. Change the code to

CODE

names[i] = malloc(100);
scanf("%s",names[i]); 

RE: receiving strings from keyboard and storing base address in array of pointers...

Also, your array is too small. Your loop goes from 0 to 6 which is a total of 7 iterations of the loop. Your array "names[]" is only 6 long.


RE: receiving strings from keyboard and storing base address in array of pointers...

(OP)
the code that i have given is not giving any compile time error.but at execution time it is failing to execute.why?

RE: receiving strings from keyboard and storing base address in array of pointers...

Your original code is for 0 to 5, stated as 0 < 6, so the array size of 6 is good.

Jim Asman
http://www.spectracolorservices.com

RE: receiving strings from keyboard and storing base address in array of pointers...

Oops, my bad. bigsmile

Read it too fast.

Failing to execute? Does it actually not execute, or does it just do nothing? That's two different things.

Do you get an error message?

RE: receiving strings from keyboard and storing base address in array of pointers...

(OP)
output screen is coming and then when i am entering a string, a window is coming and its saying that myprogram.exe has stoped working.

RE: receiving strings from keyboard and storing base address in array of pointers...

Have you made the mod I suggested? What does your program look like now?

RE: receiving strings from keyboard and storing base address in array of pointers...

(OP)
to XWB,

using ur code(modification) my program is executing well.but i want to know why my code is not doing well.

char *names[6] in this cose 'names' is an array of pointers to string.

so whwn i m using
scanf("%s",names[i]); this means that i m passing a pointer variable to scanf function.and pointer
variable holds an address so thats correct that i m passing an address to scanf function
and when i m enetering a string from keyboard then it should store the base address of
that string into that names[i]'s address.

RE: receiving strings from keyboard and storing base address in array of pointers...

A char* is just a pointer to a location: there is no where for it to store the information. It does, as you say, store the data in the address of the given string. As you have not initialized the string, it has nowhere to store it. With the mod I posted, it allocates space for program to store the information before reading in the data. So now, it has somewhere to store the data so it does not crash.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close