PDA

View Full Version : Problem with pointers and string array. C.




Cheer
Mar 18, 2008, 04:53 AM
Hello, could anyone help me with C language a little. I'm trying to write a program with pointers, which would scan some words to the array of strings and would print them on a screen. I'm new to C programming, that's why it's so difficult to deal with such a simple task :confused:

#include <stdio.h>
#include <string.h>
int main () {
char * string[3];
int i;

..............

return (0); }



Eraserhead
Mar 18, 2008, 04:56 AM
To print use printf, in Terminal type man printf, for more information. To read in, use scanf or ideally fgets type man scanf or man fgets for more information.

Cheer
Mar 18, 2008, 05:05 AM
I was trying to. But there are still some mistakes which I can't find.. :(



#include <stdio.h>
#include <string.h>

int main ()
{
char* string[3];
int j;
for(j=0;j<3;j++)
{

fgets(string[j], sizeof string[j], stdin);

}

for(j=0;j<3;j++)
{
printf("String number %d: \"%s\"\n", j, string[j]);
}
return 0;
}

mheidt
Mar 18, 2008, 06:54 AM
If you face a problem like this you need to get hold of the values by debugging, tracing etc.

F.e. what do you expect the value of
sizeof string[j]
to be?

pilotError
Mar 18, 2008, 07:27 AM
A pointer only points to a set of characters that were allocated. Since you didn't initialize the pointers to NULL, you really have no idea what they are pointing to.

You created 3 character pointers, but you haven't allocated any memory for them to point to.

You can do a couple of things, create a variable (maybe an array of n characters by 3 - a matrix if you will) that you can point the pointer to, or you can allocated memory by calling malloc within your for loop before the fgets call.

mheidt kind of pointed you in that direction.

toddburch
Mar 18, 2008, 09:07 AM
This code, as pilotError mentions, allocates an array of three pointers:

char* string[3] ;


and it is not an array of strings.

The array will be 12 bytes long; 3 entries of 4 bytes each; each pointer being 4 bytes long.

Since you don't initialize them, they will have random data in them. Since you declare these values as pointers, (via char *), your program will expect them to contain pointers.

However, when you read data via fgets(), you are reading from stdin directly into the storage pointed to by string[j], your pointer field, which, is not pointing to anything meaningful.

You could / should read into dynamically obtained storage, and put the address of that dynamic memory into your string array entry.

Todd

iSee
Mar 18, 2008, 11:07 AM
try declaring your "string" variable like this:

char string[3][100];

This way, string is a pointer to three buffers of 100 characters each.
As others pointed out, your current way declares an array of three pointers to characters, but the three pointers don't point to allocated memory until you do something to make sure they are.

The advantage of my way is that the memory is allocated for you (on the stack). The disadvantage is that the strings are fixed to some constant size (100, in this case) at compile time.

toddburch
Mar 18, 2008, 11:48 AM
And... and.... another point!

It would behoove you to use a meaningful variable name for your pointer array. "string[3]" is not a good name for the purpose you are using it for. "string_pointer[3]" or some such name would be better and be less confusing to yourself and the people who read your code.

Todd

ChrisA
Mar 18, 2008, 12:12 PM
I was trying to. But there are still some mistakes which I can't find.. :(

for(j=0;j<3;j++)
{
printf("String number %d: \"%s\"\n", j, string[j]);
}
return 0;
}

I see your error. You must think the "char *foobar[3]" is an array of three strings. No it is an array of three pointers. You need to alocate space for the actuall strings. Use eithe "srtdup", malloc, or calloc to make the space. See man pages.

Cheer
Mar 18, 2008, 05:00 PM
Thank's a lot, i did it. I just have never realized that array *string[3] is an array of pointers, and that was my biggest mistake. Thanks a lot ;)