By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,695 Members | 1,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,695 IT Pros & Developers. It's quick & easy.

Initializing an Array of Pointers to Char

P: n/a
nk
Hi,
I'm a newbie on this language. I would be very happy if you help me
about the following issue:
The code below, reads some names(strings), stores them, and stores the
addresses in the pointer array, and writes them out. But it fails and
exits the program. I guess that it's about initializing the array but i
couldn't find a way to make it ok.
--------------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <conio.h>

int main()
{
char *a[50];

int i, numb;

printf("How many entries would you like to make?\n");
scanf("%d", &numb);

printf("Enter the names please..\n");

for ( i = 0; i < numb; i++ ) {
scanf("%s", a[i]);
}

for ( i = 0; i < numb; i++ ) {
printf("%s", a[i]);
}

getch();
return 0;
}
-------------------------------------------------------------------------------------------------------------------------------------

Thanks..

Jul 29 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
nk a écrit :
Hi,
I'm a newbie on this language. I would be very happy if you help me
about the following issue:
The code below, reads some names(strings), stores them, and stores the
addresses in the pointer array, and writes them out. But it fails and
exits the program. I guess that it's about initializing the array but i
couldn't find a way to make it ok.
--------------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <conio.h>

int main()
{
char *a[50];

int i, numb;

printf("How many entries would you like to make?\n");
scanf("%d", &numb);

printf("Enter the names please..\n");

for ( i = 0; i < numb; i++ ) {
scanf("%s", a[i]);
}

for ( i = 0; i < numb; i++ ) {
printf("%s", a[i]);
}

getch();
return 0;
}
-------------------------------------------------------------------------------------------------------------------------------------

Thanks..

The problem is here
char *a[50];
Here you reserve space for 50 POINTERS to char, that
at the start of the program point nowhere. You have
failed to make those pointers point to somewhere.

Before getting the user's input, allocate space
for those 50 lines and make those 50 pointers point to the
allocated space (Hint: malloc())

After you are done with those pointers, free the allocated space
(Hint free())

jacob
Jul 29 '06 #2

P: n/a
[This followup was posted to comp.lang.c]

While wandering through cyberspace on 29 Jul 2006 11:48:44 -0700, nk
said ...
Hi,
I'm a newbie on this language. I would be very happy if you help me
about the following issue:
The code below, reads some names(strings), stores them, and stores the
addresses in the pointer array, and writes them out. But it fails and
exits the program. I guess that it's about initializing the array but i
couldn't find a way to make it ok.
--------------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <conio.h>

int main()
{
char *a[50];

int i, numb;

printf("How many entries would you like to make?\n");
scanf("%d", &numb);

printf("Enter the names please..\n");

for ( i = 0; i < numb; i++ ) {
scanf("%s", a[i]);
}

for ( i = 0; i < numb; i++ ) {
printf("%s", a[i]);
}

getch();
return 0;
}
-------------------------------------------------------------------------------------------------------------------------------------
You need to initialize the array of pointers before you use it.

There are several ways to do this. You can either initialize each
element of the array to a the result of a call to malloc() or assign the
address of a static area to each element of the array.
Jul 29 '06 #3

P: n/a
nk wrote:
Hi,
I'm a newbie on this language. I would be very happy if you help me
about the following issue:
The code below, reads some names(strings), stores them, and stores the
addresses in the pointer array, and writes them out. But it fails and
exits the program. I guess that it's about initializing the array but i
couldn't find a way to make it ok.
--------------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <conio.h>
Why include conio.h? It's a non-standard header and there are better
ways of ensuring you see the output of your program than calling getch.
int main()
Better to be explicit about not taking parameters.
int main(void)
{
char *a[50];

int i, numb;

printf("How many entries would you like to make?\n");
scanf("%d", &numb);
What of the user types "one" instead of "1"? Always check the return
value of input functions and the *scanf family of functions. Although in
general it is easier to read the input with fgets (NOT gets, NEVER gets)
and then parse it.
printf("Enter the names please..\n");

for ( i = 0; i < numb; i++ ) {
scanf("%s", a[i]);
Don't use a %s in scanf without specifying an upper limit. It's like
holding a shot glass over your computer, giving the user a hose,
blindfolding him/her so s/he does not know how large the glass is, and
then saying pour in as much water as you want. At some point someone
will flood your computer completely wrecking it. gets has *exactly* the
same problem as the line above.

You have the additional problem that a[i] is a pointer to char which
does not point anywhere in particular yet. So scanf will write to some
random part. scanf does not magically create space for the string it is
reading, you need to do that before you call it. This also means you
need to decide on an amount of space and tell scanf how much space
you've given it.

Further, you are not checking that the user has input a number <= 50.
Having fixed all the other problems, the user could still enter 51 and
blow up your computer.
}

for ( i = 0; i < numb; i++ ) {
printf("%s", a[i]);
}

getch();
On my system that function sends insulting messages to the Chinese
embassy. You could use a standard input function or find out how to get
your environment not to close the window as soon as the program terminates.
return 0;
}
-------------------------------------------------------------------------------------------------------------------------------------

Thanks..
All my comments are to help you learn even though many of them don't
address your immediate problem.
--
Flash Gordon
Still sigless on this computer
Jul 29 '06 #4

P: n/a
nk
jacob navia, Comcast News and Flash Gordon, thank you so very much!
i've almost fixed the problem by your help. it's great to take a
helping hand when you're a newbie. thanks a lot.
Flash Gordon, could you please give some examples (alternatives) about
that issue:

"Why include conio.h? It's a non-standard header and there are better
ways of ensuring you see the output of your program than calling
getch."

also, it would be very helpful if you inform me a little more about
specifying an upper limit for scanf when using a %s.

Thanks a lot !..

Jul 30 '06 #5

P: n/a
On 2006-07-30, nk <wh***************@hotmail.comwrote:
jacob navia, Comcast News and Flash Gordon, thank you so very much!
i've almost fixed the problem by your help. it's great to take a
helping hand when you're a newbie. thanks a lot.
Flash Gordon, could you please give some examples (alternatives) about
that issue:

"Why include conio.h? It's a non-standard header and there are better
ways of ensuring you see the output of your program than calling
getch."

also, it would be very helpful if you inform me a little more about
specifying an upper limit for scanf when using a %s.
printf ("\n");
fflush (stdout);

Either of those will work.
(Unless you can't see your output because you're in Visual Studio.
The fix in that case is to stop using Visual Studio.)

--
Andrew Poelstra <website down>
To reach my email, use <email also down>
New server ETA: 42
Jul 31 '06 #6

P: n/a
Andrew Poelstra said:

<snip>
printf ("\n");
fflush (stdout);

Either of those will work.
(Unless you can't see your output because you're in Visual Studio.
The fix in that case is to stop using Visual Studio.)
No, the fix in that case is to run the program in the debugger, putting a
breakpoint on the return statement in main() - another great reason to have
exactly one exit point in your program - or to run it outside the debugger
but still within Visual Studio (which will say "press any key to continue"
at the end of the program), or to run it from a console.

I am far from being the world's greatest Microsoft fan, but there's very
little wrong with their C compiler and IDE.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 31 '06 #7

P: n/a
On 2006-07-31, Richard Heathfield <in*****@invalid.invalidwrote:
Andrew Poelstra said:

<snip>
>printf ("\n");
fflush (stdout);

Either of those will work.
(Unless you can't see your output because you're in Visual Studio.
The fix in that case is to stop using Visual Studio.)

No, the fix in that case is to run the program in the debugger, putting a
breakpoint on the return statement in main() - another great reason to have
exactly one exit point in your program - or to run it outside the debugger
but still within Visual Studio (which will say "press any key to continue"
at the end of the program), or to run it from a console.

I am far from being the world's greatest Microsoft fan, but there's very
little wrong with their C compiler and IDE.
I have nothing against their compiler; I just don't like IDE's in
general. What I meant by "Stop using VS" was to go into a command line
and invoke the compiler directly. Or, you could keep using the IDE and
use a command line to run the program directly. (Either way, it's a good
experience if only to teach you something about command lines.)

--
Andrew Poelstra <website down>
To reach my email, use <email also down>
New server ETA: 42
Jul 31 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.