Initializing an Array of Pointers to Char

N

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);
}

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

getch();
return 0;
}
 
J

jacob navia

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);
}

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

getch();
return 0;
}



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
 
C

Comcast News

[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);
}

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

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.
 
F

Flash Gordon

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.

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);


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 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);
}

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;
}

All my comments are to help you learn even though many of them don't
address your immediate problem.
 
N

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 !..
 
A

Andrew Poelstra

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.)
 
R

Richard Heathfield

Andrew Poelstra said:

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.
 
A

Andrew Poelstra

Andrew Poelstra said:



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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top