473,785 Members | 2,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

segfault on strtok

Hi,

.. following causes a segfault. .. didn't
know why ?!

int main() {
char name[15];
strcpy (name, "ab8bc8cd8e d");

char cur[800];
strcpy (cur, strtok(name, "8"));

while (cur) {
printf ("Output: %s\n", cur);
printf ("Stringleng th %i\n", strlen(cur));
strcpy(cur,strt ok(0, "8"));
printf ("next\n");
}

}

output: -----------------------

Output: ab
Stringlength 2
next
Output: bc
Stringlength 2
next
Output: cd
Stringlength 2
next
Output: ed
Stringlength 2
Segmentationfau lt
------------------------------

Thanks for any help
Regards
Fatih Gey
Nov 13 '05
40 3525


Fatih Gey wrote:
Hi,

.. following causes a segfault. .. didn't
know why ?!

int main() {
char name[15];
strcpy (name, "ab8bc8cd8e d");

char cur[800];
strcpy (cur, strtok(name, "8"));

while (cur) {
printf ("Output: %s\n", cur);
printf ("Stringleng th %i\n", strlen(cur));
strcpy(cur,strt ok(0, "8"));
printf ("next\n");
}

}


Once all the delimiters, '8' in this case, are exhausted, function
strtok will return NULL. At the point of the seg fault your
statement:
strcpy(cur,strt ok(0,"8"));
becomes
strcpy(cur,NULL );
The second argument NULL is not defined for function strcpy.
The argument must be a string. Therein lies your seg fault.

Actually, if your intent is to only print the tokens, you should
just use a char * instead of using the character array cur.

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

int main(void)
{
char name[15], *cur;

strcpy (name, "ab8bc8cd8e d");
cur = strtok(name, "8");
while(cur)
{
printf ("Output: %s\n", cur);
printf ("Stringleng th %u\n", strlen(cur));
printf ("next\n");
cur = strtok(NULL,"8" );
}
}

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapi dsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 13 '05 #11
In article <3F***********@ mindspring.com> , pete wrote:
Andreas Kahari wrote:
Yes, the integer zero is a null pointer, but only if it's cast
to void * and converted into a pointer type.


A constant expression with a value of zero,
is called a "null pointer constant"
(you can find that phrase in the standard),
whther or not it's cast to (void*).


I was looking at the standard when I wrote it. It says that an
integer which is zero is a null pointer constant if cast to void *.
That null pointer constant is a null pointer if converted to
a pointer type.

--
Andreas Kähäri
Nov 13 '05 #12
Andreas Kahari wrote:

In article <3F***********@ mindspring.com> , pete wrote:
Andreas Kahari wrote:
Yes, the integer zero is a null pointer, but only if it's cast
to void * and converted into a pointer type.


A constant expression with a value of zero,
is called a "null pointer constant"
(you can find that phrase in the standard),
whther or not it's cast to (void*).


I was looking at the standard when I wrote it. It says that an
integer which is zero is a null pointer constant if cast to void *.
That null pointer constant is a null pointer if converted to
a pointer type.


I read it differently from the way that you do.

N869
6.3.2.3 Pointers
[#3]
An integer constant expression with the value 0,
or such an expression cast to type void *,
is called a null pointer constant.

--
pete
Nov 13 '05 #13
In article <3F**********@m indspring.com>, pete wrote:
Andreas Kahari wrote:

[cut]
I was looking at the standard when I wrote it. It says that an
integer which is zero is a null pointer constant if cast to void *.
That null pointer constant is a null pointer if converted to
a pointer type.

I read it differently from the way that you do.

N869
6.3.2.3 Pointers
[#3]
An integer constant expression with the value 0,
or such an expression cast to type void *,
is called a null pointer constant.

That's the draft standard. I have the book version of the
current standard, but it's at home at the moment. I'll have
to get back to this issue later tonight if noone else has the
current standard at hand and can verify this.
Cheers,
Andreas

--
Andreas Kähäri
Nov 13 '05 #14


Andreas Kahari wrote:
In article <3F***********@ mindspring.com> , pete wrote:
Andreas Kahari wrote:

Yes, the integer zero is a null pointer, but only if it's cast
to void * and converted into a pointer type.


A constant expression with a value of zero,
is called a "null pointer constant"
(you can find that phrase in the standard),
whther or not it's cast to (void*).

I was looking at the standard when I wrote it. It says that an
integer which is zero is a null pointer constant if cast to void *.
That null pointer constant is a null pointer if converted to
a pointer type.


I believe what you are referring to in the standard is the
section 6.3.2.3 Pointers.
Paragraph:
3. An integer constant expression with the value 0, or such an
expression cast to type void *, is called a null pointer constant.

Notice the word "or". It does not say "only if cast".
Among other posibilities, NULL may be defined as:
0, 0L, 0LL, or (void *)0.

---
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapi dsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 13 '05 #15
On Fri, 24 Oct 2003 12:55:34 +0000, Andreas Kahari wrote:
In article <3F**********@m indspring.com>, pete wrote:
Andreas Kahari wrote:

[cut]
I was looking at the standard when I wrote it. It says that an
integer which is zero is a null pointer constant if cast to void *.
That null pointer constant is a null pointer if converted to
a pointer type.

I read it differently from the way that you do.

N869
6.3.2.3 Pointers
[#3]
An integer constant expression with the value 0,
or such an expression cast to type void *,
is called a null pointer constant.

That's the draft standard. I have the book version of the
current standard, but it's at home at the moment. I'll have
to get back to this issue later tonight if noone else has the
current standard at hand and can verify this.


ISO/IEC 9899:1999
6.3.2.3 Pointers
3 An integer constant expression with the value 0, or such an
expressioncast to type void *, is called a null pointer
constant.
Nov 13 '05 #16
"Fatih Gey" <fa*******@2way .de> wrote:
...
char cur[800];
strcpy (cur, strtok(name, "8"));

while (cur) {
This is a constant loop -- i.e., the loop will never end.
printf ("Output: %s\n", cur);
printf ("Stringleng th %i\n", strlen(cur));
strcpy(cur,strt ok(0, "8"));
strtok eventually will return NULL. strcpy cannot accept a parameter which is NULL.
printf ("next\n");
}
...


--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/
Nov 13 '05 #17
In article <pa************ *************** *@yahoo.com>, Sheldon Simms wrote:
On Fri, 24 Oct 2003 12:55:34 +0000, Andreas Kahari wrote:

[cut]
That's the draft standard. I have the book version of the
current standard, but it's at home at the moment. I'll have
to get back to this issue later tonight if noone else has the
current standard at hand and can verify this.

ISO/IEC 9899:1999
6.3.2.3 Pointers
3 An integer constant expression with the value 0, or such an
expressioncast to type void *, is called a null pointer
constant.

Yeah yeah, alright.

I wonder what they mean by "or such an expression" though...
--
Andreas Kähäri
Nov 13 '05 #18
In <sl************ **********@vinl and.freeshell.o rg> Andreas Kahari <ak*******@free shell.org> writes:
In article <3F**********@m indspring.com>, pete wrote:
Andreas Kahari wrote:

[cut]
I was looking at the standard when I wrote it. It says that an
integer which is zero is a null pointer constant if cast to void *.
That null pointer constant is a null pointer if converted to
a pointer type.

I read it differently from the way that you do.

N869
6.3.2.3 Pointers
[#3]
An integer constant expression with the value 0,
or such an expression cast to type void *,
is called a null pointer constant.


That's the draft standard.


It doesn't matter: the text hasn't changed at least since the last
public C89 draft, released 15 years ago.

It makes sense to object to N869 quotations *only* when you know that
either C89 or C99 say something different.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #19
In article <bn**********@s unnews.cern.ch> , Dan Pop wrote:
In <sl************ **********@vinl and.freeshell.o rg> Andreas
Kahari <ak*******@free shell.org> writes:

[cut]
That's the draft standard.


It doesn't matter: the text hasn't changed at least since the last
public C89 draft, released 15 years ago.

It makes sense to object to N869 quotations *only* when you know that
either C89 or C99 say something different.

I agree, but I thought I remembered the text differently.
Others proved me wrong though.
--
Andreas Kähäri
Nov 13 '05 #20

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

Similar topics

7
5965
by: James Leddy | last post by:
For some reason, I get a segmentation fault when I exit this program I made. Most of the time when I have seen this it has occured in the middle of the program and the program terminates. However, in this instance, the program gets a segmentation fault on exit. I already posted this question, but people were asking for some code, so here it is: #define DWORD unsigned long
13
4927
by: ern | last post by:
I'm using strtok( ) to capture lines of input. After I call "splitCommand", I call strtok( ) again to get the next line. Strtok( ) returns NULL (but there is more in the file...). That didn't happen before 'splitCommands' entered the picture. The problem is in splitCommands( ) somehow modifying the pointer, but I HAVE to call that function. Is there a way to make a copy of it or something ? /* HERE IS MY CODE */ char *...
8
1931
by: hu | last post by:
hi, everybody! I'm testing the fuction of strtok(). The environment is WinXP, VC++6.0. Program is simple, but mistake is confusing. First, the below code can get right outcome:"ello world, hello dreams." #include <stdafx.h> #include <string.h> #include <stdio.h> int main()
4
2735
by: Michael | last post by:
Hi, I have a proble I don't understand when using strtok(). It seems that if I make a call to strtok(), then make a call to another function that also makes use of strtok(), the original call is somehow confused or upset. I have the following code, which I am using to tokenise some input which is in th form x:y:1.2: int tokenize_input(Sale *sale, char *string){
3
3811
by: nomad5000 | last post by:
Hi everybody! I'm having trouble using strtok to fill a matrix with int nrs. from a file. the code that is not working is the following: #include <iostream> #include <fstream> #include <string> #include <stdlib.h> using namespace std;
29
2587
by: Pietro Cerutti | last post by:
Hello, here I have a strange problem with a real simple strtok example. The program is as follows: ### BEGIN STRTOK ### #include <string.h> #include <stdio.h>
11
17178
by: magicman | last post by:
can anyone point me out to its implementation in C before I roll my own. thx
12
2360
by: Pilcrow | last post by:
Here is a quick program, together with its output, that illustrates what I consider to be a deficiency of the standard function strtok from <string.h>: I am using C:\>gcc --version gcc (GCC) 3.4.5 (mingw special) I would like there to be a default, to be returned when two delimiters
2
1295
by: aviraldg | last post by:
I'm having a SEGFAULT on the following program. Weird thing is that it happens randomly with different "str"s (ie. user input; check main() for more info) and my debugger shows me that the getToken function is working and returning the correct answer. #include <stdio.h> #include <string.h> #include <ctype.h> char* getToken(char *src, unsigned int pos=0) { char *token=new char; unsigned int i=0;
0
9646
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9484
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
10350
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
8983
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7505
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
5386
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
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.