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 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
"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.
"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
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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*/
|
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...
|
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)...
|
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;
|
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.
| |
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...
|
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...
|
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:
...
|
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;
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |