So for my assignment, I'm supposed to fill ten strings from a file using an array of pointers. Now I know I'm doing this wrong since my compiler is telling me that there are errors on the levels of indirection. Pointers are so confusing... >_>

char* pStrings[10];
char** pWalker;
FILE* spIn;

spIn = fopen("numbers.txt", "r");

while(fgets(pStrings, sizeof(pStrings), spIn))
     printf("%s", pStrings);
printf("\n");

Well it technically still works, but given the errors from my compiler I think I'm doing something wrong. Do I need another array for the pStrings to be referenced to?

>while(fgets(pStrings, sizeof(pStrings), spIn))
That's bad on several levels. Now, I could tell you why it works, but I'd rather tell you why it's wrong. pStrings is an array of pointers to char, but fgets expects a single pointer to char. At the very least you should be passing pString as the first argument where i is something between 0 and 10.

But, because pString is a pointer and not an array, you can't use sizeof(pString) to get the allocated size. Finally, you need to allocate memory to each pointer, because writing to an uninitialized pointer is a Bad Thing(TM).

>Well it technically still works,
Serious trouble ahead if you really think so. char* pStrings[10]; There are 10 pointers to char there, but none has been initialized to a chunk of memory.
First is necessary to set aside some memory for each pointer to point to, using the function malloc.

e.g pStrings[0] = malloc( 10 ); Then you pass to fgets only one string at a time. fgets( pString[0], 10, stream ); /* 10 is the size of the memory */ After you're done with the memory, you need to let it free. ;) free( pString[0] );

Ah, I should have read the question more carefully. I can receive the data with fgets, and then use an array of pointer AFTERWARDS. I mean, I guess it's the only way when an array of pointers is not initialized.

What I'm still sort of confused with is how to fill ten strings from a file using an array of pointers.

I've tried using fgets one pointer at a time, but it's not working for some reason...

for(i=0;i<10;i++)
    fgets(pStrings[i], sizeof(pStrings), spIn);
for(i=0;i<10;i++)
    printf("%s\n", pStrings[i]);

fgets(pStrings[i], sizeof(pStrings), spIn); What it is in red is the size of the 10 pointers, and since each pointer variable is normally 4 bytes, you are reading into pString 40 characters.


/* establish the length of the strings */
#define LINE 50 

in main:
char *pString[10]

loop:
    pString[i] = malloc( LINE )
    if( pString[i] == NULL )
    {
           fprintf( stderr, "Error allocating memory" );
           /* Handle the error properly */
    }
loop:
     fgets( pString[i], LINE, stream );

you can also do it like this:

char inbuf[255];
// allocate array of 10 pointers and initialize them all to 0
char* pString[10] = {0};
int i;

for(i = 0; i < 10 && fgets(inbuf, sizeof(inbuf), spIn+i)
{
    // allocate memory for the string
    pString[i] = malloc(strlen(inbuf+1);
    // copy the string to the pString array
    strcpy(pString[i], inbuf);
}

for loops generally have three sections. You've forgotten the increment section. :)

You're also missing a closing parentheses on the strlen() call . . . .

But we know what you mean. :)

char inbuf[255];
// allocate array of 10 pointers and initialize them all to 0
char* pString[10] = {0};
int i;

for(i = 0; i < 10 && fgets(inbuf, sizeof(inbuf), spIn+i); i ++)
{
    // allocate memory for the string
    pString[i] = malloc(strlen(inbuf)+1);
    // copy the string to the pString array
    strcpy(pString[i], inbuf);
}

[edit] That's just weird. I put [color=blue] there. It's green. [/edit]

you are right -- my version was incorrect, and so is yours. spIn+1 should just be spIn -- don't add +1 to the stream pointer. for(i = 0; i < 10 && fgets(inbuf, sizeof(inbuf), spIn); i++) That's what happens when I didn't actually test the code I posted :$

This article has been dead for over six months. Start a new discussion instead.