473,811 Members | 2,779 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Program repeats itself, pointer trouble I suspect.

Hello all!
I wrote program with a array of pointers, and I suspect they are
pointing at each other in the Do ...While loop.
Something is messed up with the increment variable word. A program
clip of what I'm talking about.

#include <stdio.h>
#include <string.h>

int main(void)
{
char string[50] = {"Have a nice day folks"};
char *line_ptr;
char *list[20] = { '\0' }; //Initialize the array of pointers to
NULL.
int word = 0;

line_ptr = strtok(string, " ");

do
{
list[word] = line_ptr;
word++;
line_ptr = strtok(NULL," ");
} while (line_ptr != NULL);

return 0;
}
It's kinda weird, cause the program repeats itself when I run it. I
left out the output section, cause I know it works fine. In the
debugger it works OK. Is there a memory leak? Do you need to assign
pointers to NULL after you use them? Should I free up the memory from
the array of pointers before the program terminates? What is the best
way handle pointers after your done with them?

Thanks for all your help
Neil

Feb 15 '07
27 2182
On Feb 14, 7:24 pm, Barry Schwarz <schwa...@doezl .netwrote:
On 14 Feb 2007 16:50:11 -0800, "Neil" <neilwrit...@ho tmail.comwrote:


Hello all!
I wrote program with a array of pointers, and I suspect they are
pointing at each other in the Do ...While loop.
Something is messed up with the increment variable word. A program
clip of what I'm talking about.
#include <stdio.h>
#include <string.h>
int main(void)
{
char string[50] = {"Have a nice day folks"};
char *line_ptr;
char *list[20] = { '\0' }; //Initialize the array of pointers to
NULL.

This is why you should not use // style comments in usenet.

If you want assign each of the 20 pointer in the array the NULL value,
use NULL. While 0 and '\0' will both work, they are visually
misleading. Someone might be tempted to think that the pointers point
to a char containing '\0'.
int word = 0;
line_ptr = strtok(string, " ");
do
{
list[word] = line_ptr;
word++;
line_ptr = strtok(NULL," ");
} while (line_ptr != NULL);
return 0;
}
It's kinda weird, cause the program repeats itself when I run it. I
left out the output section, cause I know it works fine. In the

Define repeat.
It starts again from main(), There must be problem elsewhere.
-Neil

Feb 16 '07 #11
On Feb 15, 7:56 am, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
"Neil" <neilwrit...@ho tmail.comwrites :
#include <stdio.h>
#include <string.h>
int main(void)
{
char string[50] = {"Have a nice day folks"};
char *line_ptr;
char *list[20] = { '\0' }; /* Initialize the array of pointers to NULL.*/
int word = 0;
line_ptr = strtok(string, " ");
do
{
list[word] = line_ptr; /* MARK */
word++;
line_ptr = strtok(NULL," ");
} while (line_ptr != NULL);

Other than and hint in a reply to another reply, no one has pointed
out that you are in danger of accessing outside the bounds of your
array "list".

If it reasonable (is it ever?) to simply discard tokens that don't
fit, you can write:

if (word < 20) list[word] = line_ptr;

where I put /* MARK */ in your code.

I don't know of this is the source of your problem, because the
description is rather vague.

--
Ben.- Hide quoted text -

- Show quoted text -
Ben your probably right, but I didn't want to go through whole
program, just the code clip I
mentioned. Thanks for the help, I'll try that....

-Neil
Feb 16 '07 #12
On Feb 14, 8:45 pm, "Beej" <b...@beej.uswr ote:
On Feb 14, 4:50 pm, "Neil" <neilwrit...@ho tmail.comwrote:
line_ptr = strtok(string, " ");

Unless you're sure you're always going to have a token, you should
probably check line_ptr for NULL here, too.

Here's a compact form of that, if you don't mind assignments in your
expressions:

if ((p = strtok(string, " ")) != NULL) {
do {
printf("Token: %s\n", p);
} while ((p = strtok(NULL, " ")) != NULL);

}
do
{
list[word] = line_ptr;
word++;
line_ptr = strtok(NULL," ");
} while (line_ptr != NULL);

Here is my output when I ran it.

'Have'
'a'
'nice'
'day'
'folks'

Looks fine to me. What's your output?

-Beej
Beej the output section is a function that I left off, however the
program does begin
at the start, or the program crashes, from what I tried recently.
I'm trying to isolate the problem. thanks.
-Neil.

Feb 16 '07 #13
"Old Wolf" <ol*****@inspir e.net.nzwrites:
On Feb 16, 12:46 pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
>"Old Wolf" <oldw...@inspir e.net.nzwrites:
Are you perhaps referring to the fact that the program might
break if its source is modified to introduce a bug, as
suggested by Ben Bacarisse?

I'm not seeing it. Can you post a correction to my message (or
explain it to me and I'll post a correction)?

Your message appears to be saying that the program could
break if the input string were modified to have more than
20 words in it, which would be a bug. But the original
post only had 4 words in the string, so there is no problem.
Ah, OK. Crossed wires.
(Of course it is not a bad idea to add in checking, as
you suggested).
....but probably not in any way a fix for the OP's problem if it failed
with a four token string.

--
Ben.
Feb 16 '07 #14
On Feb 14, 7:24 pm, Barry Schwarz <schwa...@doezl .netwrote:
This is why you should not use // style comments in usenet.
Huh?....Didn't know that.

Barry, I'm using an old Borland Turbo C++ for DOS Compiler to write
this program, it's crashed a couple of times writing I what I think
what looks like elementry code.

If you don't intialize a pointer, but you declared it main() and just
leave it.
Does this present a problem after the program terminates?

Can two char pointers in an array of pointers point to each other
after the program terminates?

Thanks for tip..

-Neil

Feb 16 '07 #15
On Feb 15, 7:18 pm, "Neil" <neilwrit...@ho tmail.comwrote:
On Feb 14, 7:24 pm, Barry Schwarz <schwa...@doezl .netwrote:
This is why you should not use // style comments in usenet.

Huh?....Didn't know that.

Barry, I'm using an old Borland Turbo C++ for DOS Compiler to write
this program, it's crashed a couple of times writing I what I think
what looks like elementry code.

If you don't intialize a pointer, but you declared it main() and just
leave it.
I ment inside main() :)

-Neil
Feb 16 '07 #16
On Feb 14, 9:00 pm, Christopher Layne <cla...@com.ano dizedwrote:
Neil wrote:
#include <stdio.h>
#include <string.h>
int main(void)
{
char string[50] = {"Have a nice day folks"};
char *line_ptr;
char *list[20] = { '\0' }; //Initialize the array of pointers to

Cleaner version:

#include <stdio.h>
#include <string.h>

enum constants {
STRING_MAX = 50,
WORD_MAX = 20

};

int main(void)
{
char string[STRING_MAX] = "Have a nice day folks";
char *word[WORD_MAX];
int i;

for (i = 0; i < WORD_MAX; i++) {
if ((word[i] = strtok(i ? NULL : string, " ")) == NULL)
break;
fprintf(stdout, "word[%d] = %s\n", i, word[i]);
}

return 0;
Huh? ........Better then mine..........b ut OK.
I'll try that.
Thanks Chris..
-Neil

Feb 16 '07 #17
On 15 Feb 2007 18:18:01 -0800, "Neil" <ne*********@ho tmail.comwrote:
>On Feb 14, 7:24 pm, Barry Schwarz <schwa...@doezl .netwrote:
This is why you should not use // style comments in usenet.
Huh?....Didn 't know that.

Barry, I'm using an old Borland Turbo C++ for DOS Compiler to write
this program, it's crashed a couple of times writing I what I think
what looks like elementry code.
Post a compilable example of the code that exhibits the behavior.
>
If you don't intialize a pointer, but you declared it main() and just
leave it.
Does this present a problem after the program terminates?
Unless you attempt to evaluate its value, the fact that a pointer is
uninitialized (technically called indeterminate) cannot be a problem.
>
Can two char pointers in an array of pointers point to each other
after the program terminates?
Unless you have a very strange operating system, after your program
terminates none of your objects exist. Since they no longer exist,
one of the many things they don't do is point anywhere, let alone to
each other.

While your program is running, it is still not possible for one char
pointer to point to another. By definition, a char pointer must point
to a char (if it points anywhere). It makes no difference whether the
pointers are in an array or not.
Remove del for email
Feb 16 '07 #18
On Feb 15, 9:28 pm, Barry Schwarz <schwa...@doezl .netwrote:
Post a compilable example of the code that exhibits the behavior.

Barry, I would if I knew where the problem is in the program..
Obviouly the pointers are not causing the problem.

Thanks for all your help
-Neil
Feb 16 '07 #19
Neil said:
On Feb 15, 9:28 pm, Barry Schwarz <schwa...@doezl .netwrote:
Post a compilable example of the code that exhibits the behavior.

Barry, I would if I knew where the problem is in the program..
If you knew where the problem was, you wouldn't need to ask about it at
all. Since you don't know where the problem was, it makes sense to post
the whole program.
Obviouly the pointers are not causing the problem.
But you just said you don't know where the problem is.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 16 '07 #20

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

Similar topics

11
3532
by: Damon | last post by:
Hi, Can someone point out to me what's wrong with the code below? I'm trying to reuse the deleted pointer but it won't compile. My reason may be wrong, but I thought reusing the pointer would save the trouble of declaring a pointer to a string array. I also suspect there will be a problem of releasing memory in the way I'm using the array pointer and is toying with using a vector<std::string> instead. Anyway, thanks for any pointers...
33
4895
by: dough | last post by:
Is it possible in C to declare and initialize a pointer that points to itself? Why or why not?
26
1566
by: Albert | last post by:
how do i write a program that outputs its input if its input is more than 80 characters?
5
3423
by: shanknbake | last post by:
Here is my code. I've noted where the program crashes. I'm doing this program as a project for school. //cqueue.h file //HEADER FILE http://rafb.net/paste/results/Nh0aLB77.html -------------------------------------------------------------------- //cqueue.cpp file //IMPLEMENTATION OF cqueue CLASS http://rafb.net/paste/results/A2gXAr73.html
20
2627
by: Francine.Neary | last post by:
I am learning C, having fun with strings & pointers at the moment! The following program is my solution to an exercise to take an input, strip the first word, and output the rest. It works fine when you give it 2 or more words, but when there's only 1 word the results vary depending on whether it's on Windows or Linux: under MSVC it displays no output (as it should); under gcc/Linux it instead gives "Segmentation fault". Any ideas...
5
2376
by: andi | last post by:
Hello, I created a report in Acess2000 and now it repeats itself three times. At first I thought its a problem with the page margins but that would mean that empty or almost empty pages would follow the initial report! In my case the whole report repeats itself completely. I hope that someone can help me on that cheers
69
5598
by: raylopez99 | last post by:
They usually don't teach you in most textbooks I've seen that delegates can be used to call class methods from classes that are 'unaware' of the delegate, so long as the class has the same signature for the method (i.e., as below, int Square (int)). Here is an example to show that feature. Note class "UnAwareClass" has its methods Square and Cuber called by a class DelegateClass. This is because these methods in UnAwareClass have the...
0
9607
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10663
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10401
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10416
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10138
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7676
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3881
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3029
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.