Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PASSING AN ARRAY OF POINTERS 1

Status
Not open for further replies.

PowerNuke

Programmer
Oct 7, 2001
10
0
0
US
Hi,

I'm having problems passing a string array, modifying it, and getting it back into main. Here are the sections of code that I think are relavent:

Function Prototype:

int fillArray(char *[], int);

Array declaration:

char *zipFileNames[ARRAYSIZELIMIT];

Function call:

numberOfFiles = fillArray(zipFileNames, ARRAYSIZELIMIT);

Array processing in function:

fileNameArray[fileNumber] = fileName;
printf("FileNumber %d is filename: %s\n", fileNumber, fileNameArray[fileNumber]);
fileNumber = fileNumber + 1;

Please note that the printf statement is there as a debugging tool, and that it shows that the array is filling properly.

main() statements following function call:

printf("Provide the array index (0 - %d):\n", (numberOfFiles - 1));
scanf("%d", &userPrintAnswer);
if (userPrintAnswer >= 0 && userPrintAnswer < numberOfFiles)
printf(&quot;The filename is: %s\n&quot;, *zipFileNames[userPrintAnswer]);

Unfortunately, the last line produces:

The filename is: (null)


Any help will be appreciated!

David
 
David,

Your printf should be:
Code:
printf(&quot;The filename is:  %s\n&quot;, zipFileName [userPrintAnswer]);
not,
Code:
printf(&quot;The filename is:  %s\n&quot;, *zipFileNames[userPrintAnswer]);
Note the missing asterisk.
zipFileNames is an array of character pointers.
zipFileNames[userPrintAnswer] is the userPrintAnswer'th element in that array which is a character pointer.
*zipFileNames[userPrintAnswer] is the first character in the userPrintAnswer'th element of the array.

Also in your function, fileName must be a pointer to memory which will be availble when the function returns. I.e. it cannot be a local variable in the function fillArray().


Brudnakm
 
The last line should be:
printf(&quot;The filename is: %s\n&quot;, zipFileNames[userPrintAnswer]);

Andrew Lim

Trying and exploring is adventuring
 
Hi guys,

Thanks for the info. That was clearly a problem. However, one prob still remains. I said that the array was filling properly, but it is not. The function statements are in a while loop. Each loop, the fileName is different, and as you can see, the file number increments, starting at 0.

When I change the printf statement in the function to fileNameArray[0], it still changes to print the new file name. So, I end up with a complete array of pointers, all to the single variable fileName. What I want is an array of pointers to each of the filename strings that were held by the fileName in each pass.

Suggestions?

Thanks,

David
 
David,

You then must allocate memory for each file name in your loop. This could be done using malloc. You could also allocate the space prior to calling the function by allocating a 2-D character array. This has the one drawback of forcing a maximum length on your file name strings, which if full path names are used, could become quite long.

Your alternatives are:

====== DYNAMIC ALLOCATION ========
Code:
fileNameArray[fileNumber] = malloc( strlen(fileName)+1 );
/* handle memory full error */
strcpy( fileNameArray[fileNumber], fileName );
printf(&quot;FileNumber %d is filename: %s\n&quot;,  fileNumber, fileNameArray[fileNumber]);
    fileNumber = fileNumber + 1;


====== STATIC ALLOCATION ======

Code:
char zipFileNames[ARRAYSIZELIMIT][MAX_FILE_NAME_LENGTH];

your function prototype would then change to:

int fillArray(char [][MAX_FILE_NAME_LENGTH], int);

Brudnakm


 
Brudnakm,

Thanks for all your help! It is really appreciated. I am teaching myself C, and although I'm enjoying it, I find C to be very unforgiving. It's a lot harder than 10 statement fortran, but then, I don't have to keypunch or wait overnight for my run printout. lol I had not even looked at dynamic memory allocation yet, so you got me into a new area.

Thanks again,

David
(PowerNuke)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top