Array of Pointers Question

N

NeedHelp

If anyone help point out why this code doesn't work, It would be
greatly appreciated.
------------------------------------
for(linenumber=0;fgets(line,25,filein))!=NULL;i++)
{

if((memo[linenumber]=(char *)malloc(sizeof(char)*25))==NULL)
{
printf("Alloc Fail");
exit(1);
}
}

memo[linenumber]=strtok(NULL," \n");
-------------------------------------
the result of this code puts the value returned by strtok into every
single element of memo, instead of each entry getting the new value of
strtok each time a pass in the 'for' loop is made. If anyone has any
suggestions, comments, questions, anything....?
Chris
 
M

Martin Ambuhl

NeedHelp said:
If anyone help point out why this code doesn't work, It would be
greatly appreciated.
------------------------------------
for(linenumber=0;fgets(line,25,filein))!=NULL;i++)
{

if((memo[linenumber]=(char *)malloc(sizeof(char)*25))==NULL)

First, why not explain why you posted without following the newsgroup,
checking the archives, or checking the FAQ?

If you claim to have done the foregoing, explain why you posted
uncompileable code with key variables undeclared, and with that god-awful
malloc call with the superfluous typing practice of '(char *)' and
'sizeof(char)' and the magic number '25'.
{
printf("Alloc Fail");
exit(1);

Or used 1 as an argument to exit when the only values with portably defined
meanings are 0, EXIT_SUCCESS, and EXIT_FAILURE.

Jeez.
 
M

Malcolm

NeedHelp said:
If anyone help point out why this code doesn't work, It would be
greatly appreciated.

for(linenumber=0;fgets(line,25,filein))!=NULL;i++)
This is a terrible use of the for loop. The counter i is not initialised, an
unrelated variable is, and the test condition has side effects.
{

if((memo[linenumber]=(char *)malloc(sizeof(char)*25))==NULL)
This is also very hard to read. How about splitting it up;
memo[linenumber] = malloc(25);
if(memo[linenumber])
{
}
{
printf("Alloc Fail");
Probably you mean fprintf(stderr, ...);
If not you need to flush with a newline.
exit(1);
exit(EXIT_FAILURE);

}
}

memo[linenumber]=strtok(NULL," \n");
What are you trying to achieve here? If you just want to remove the trailing
newline then strtok() isn't the intuitive way of doing it. If you want to
break up the string into tokens, you can't use memo[linenumber] since that
contains a pointer you need to free.
 
S

Sean Kenwrick

NeedHelp said:
If anyone help point out why this code doesn't work, It would be
greatly appreciated.
------------------------------------
for(linenumber=0;fgets(line,25,filein))!=NULL;i++)
{

if((memo[linenumber]=(char *)malloc(sizeof(char)*25))==NULL)
{
printf("Alloc Fail");
exit(1);
}
}

memo[linenumber]=strtok(NULL," \n");
-------------------------------------
the result of this code puts the value returned by strtok into every
single element of memo, instead of each entry getting the new value of
strtok each time a pass in the 'for' loop is made. If anyone has any
suggestions, comments, questions, anything....?
Chris

You should post a complete compilable program for your function since we
can;t see the declaration of memo[], line, fileing etc.

But one glaring error is that you strtok() statement is outside of the for
loop.

Also the first time you call strok() you should pass a pointer to the string
from which you want to take the tokens from (in this case 'line'). If you
have more tokens to extract from the string you then pass NULL as the first
parameter to strtok(). Note that strtok() modifies the string you pass
to it by inserting null bytes at the character position of each token found.
If this is not desirable behaviour you should take a copy of you string and
pass that to strtok() instead....

Sean
 
N

NeedHelp

Malcolm said:
memo[linenumber]=strtok(NULL," \n");
What are you trying to achieve here? If you just want to remove the trailing
newline then strtok() isn't the intuitive way of doing it. If you want to
break up the string into tokens, you can't use memo[linenumber] since that
contains a pointer you need to free.

Sorry for the confusion, memo was declared with "char* memo[NUMBER];"
It turned out the problem was I was trying to copy strings with the
use of the "=" operand instead of strcpy(). Any future postings i'll
be sure to explain myself better. Thanks for the help.
 

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

Similar Threads


Members online

Forum statistics

Threads
474,139
Messages
2,570,807
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top