473,511 Members | 9,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array of Pointers Question

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
Nov 14 '05 #1
4 1287
NeedHelp wrote:
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.
--
Martin Ambuhl
Nov 14 '05 #2

"NeedHelp" <cb****@albany.edu> wrote in message
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.

Nov 14 '05 #3

"NeedHelp" <cb****@albany.edu> wrote in message
news:cd**************************@posting.google.c om...
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
Nov 14 '05 #4
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote in message news:<c0**********@news5.svr.pol.co.uk>...
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.
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
8455
by: dam_fool_2003 | last post by:
Friends, cannot we malloc a array? So, I tried the following code: int main(void) { unsigned int y={1,3,6},i,j; for(i=0;i<3;i++) printf("before =%d\n",y); *y = 7; /* 1*/
8
3665
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
20
2906
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes)...
19
14492
by: gaga | last post by:
I can't seem to get this to work: #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *names; char **np;
9
15706
by: sangeetha | last post by:
Hello, Is there any performance difference in using of the following two declaration? int (*ptr); //Array of 10 int pointers int *ptr; // pointer-to-array of 10. Regards, Sangeetha.
9
2600
by: buda | last post by:
Hi, I've been wondering for a while now (and always forgot to ask :) what is the exact quote from the Standard that forbids the use of (&array) (when x >= number_of_columns) as stated in the FAQ...
6
4407
by: Piotrek | last post by:
Hi there again! Last time you helped me with pointers - it let me to save many hours of searching for some solutions. And once again I have question. Let's declare array of pointers: char...
11
1953
by: copx | last post by:
Unforuntately, I know next to nothing about ASM and compiler construction, and while I was aware of the syntactic differences between pointers and arrays, I was not aware of this: ...
6
3497
by: M Turo | last post by:
Hi, I was wondering if anyone can help. I'm want to pre-load a 'table' of function pointers that I can call using a its arrayed index, eg (non c code example) pFunc = func_A; pFunc = func_B;
17
2296
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the...
0
7148
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7430
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7089
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5072
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3217
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
790
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.