473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Strings and arrays

Jim
Hi There.

I have to create a function that will take a sentence and breakout the
words and save them into an array. For some reason, I can't get it
working. I keep getting a segmentation fault being caused by
strcpy(). Can someone please tell me what I'm doing wrong:

88 void displayPigLatin(char *str) {
89 void encode(char *sPtr);
90
91 char
92 *arrStr[MAXWORDS][WORDSIZE],
93 *tokenPtr,
94 *tmpStr;
95
96 int cnt;
97
98 strcpy(arrStr[0][0], str);
99 tokenPtr = strtok(arrStr[0][0], " ");
100 printf("%s\n", tokenPtr);
Mar 28 '08 #1
5 1396
Jim
BTW - MAXWORDS is the maximum number of words that will be broken out
and WORDSIZE is the maximum number of characters per word.
Mar 28 '08 #2
On Mar 28, 10:02*am, Jim <jim.moneyt...@gmail.comwrote:
Hi There.

I have to create a function that will take a sentence and breakout the
words and save them into an array. *For some reason, I can't get it
working. *I keep getting a segmentation fault being caused by
strcpy(). *Can someone please tell me what I'm doing wrong:

* * *88 * * void displayPigLatin(char *str) {
* * *89 * * void encode(char *sPtr);
* * *90
* * *91 * * char
* * *92 * * * * *arrStr[MAXWORDS][WORDSIZE],
* * *93 * * * * *tokenPtr,
* * *94 * * * * *tmpStr;
* * *95
* * *96 * * int cnt;
* * *97
* * *98 * * strcpy(arrStr[0][0], str);
* * *99 * * tokenPtr = strtok(arrStr[0][0], " ");
* * 100 * * printf("%s\n", tokenPtr);
arrStr[0][0] has not yet been set to point to any storage location.
When you copy str to it, you are copyihng to some random place,
corrupting memory.
--
Fred Kleinschmidt
Mar 28 '08 #3
Jim
On Mar 28, 1:09 pm, fred.l.kleinschm...@boeing.com wrote:
On Mar 28, 10:02 am, Jim <jim.moneyt...@gmail.comwrote:
Hi There.
I have to create a function that will take a sentence and breakout the
words and save them into an array. For some reason, I can't get it
working. I keep getting a segmentation fault being caused by
strcpy(). Can someone please tell me what I'm doing wrong:
88 void displayPigLatin(char *str) {
89 void encode(char *sPtr);
90
91 char
92 *arrStr[MAXWORDS][WORDSIZE],
93 *tokenPtr,
94 *tmpStr;
95
96 int cnt;
97
98 strcpy(arrStr[0][0], str);
99 tokenPtr = strtok(arrStr[0][0], " ");
100 printf("%s\n", tokenPtr);

arrStr[0][0] has not yet been set to point to any storage location.
When you copy str to it, you are copyihng to some random place,
corrupting memory.
--
Fred Kleinschmidt
so how can I fix that?
Mar 28 '08 #4
Jim <ji***********@gmail.comwrites:
On Mar 28, 1:09 pm, fred.l.kleinschm...@boeing.com wrote:
>On Mar 28, 10:02 am, Jim <jim.moneyt...@gmail.comwrote:
I have to create a function that will take a sentence and breakout the
words and save them into an array. For some reason, I can't get it
working. I keep getting a segmentation fault being caused by
strcpy(). Can someone please tell me what I'm doing wrong:
88 void displayPigLatin(char *str) {
89 void encode(char *sPtr);
90
91 char
92 *arrStr[MAXWORDS][WORDSIZE],
93 *tokenPtr,
94 *tmpStr;
95
96 int cnt;
97
98 strcpy(arrStr[0][0], str);
99 tokenPtr = strtok(arrStr[0][0], " ");
100 printf("%s\n", tokenPtr);

arrStr[0][0] has not yet been set to point to any storage location.
When you copy str to it, you are copyihng to some random place,
corrupting memory.
--
Fred Kleinschmidt
best not to quote sigs...
so how can I fix that?
By not doing it! Ok, glib, but how can anyone guess how you intended
this to work? You might have intended to copy each word into the
WORDSIZE char spaces in the arrStr variable. In that case the
declaration is wrong (you don't need the *).

You might have intended the arrStr to be an array of pointers, but
only a 1D array (then the * is right and the [WORDSIZE] is wrong).
The plan might have been to point to each word returned by strtok.

In both cases the initial call to strcpy seems wrong. It is a good
idea to copy the string before letting strtok loose on it, but the
copy as you have it seems out of place.

I would copy the string counting words as I go. Then I'd allocate
(with malloc) an array of pointers big enough for the job (we now
know having counted) and then I'd scan again, setting each of the
pointers to the right place in the copied string and setting the first
' ' to '\0' as I go. I would not bother with strtok at all.

--
Ben.
Mar 28 '08 #5
Jim wrote:
>

I have to create a function that will take a sentence and breakout
the words and save them into an array. For some reason, I can't
get it working. I keep getting a segmentation fault being caused
by strcpy(). Can someone please tell me what I'm doing wrong:
Lots of things. Consider a more flexible structure. Take a look
at how id2id-20 works. This effectively translates arbitrary words
into other arbitrary words. Available at:

<http://cbfalconer.home.att.net/download/>

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com

Mar 28 '08 #6

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

Similar topics

17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
5
by: Robert | last post by:
Hi, Is there some way of using an array of strings? Like in basic? I know you have to create an array of chars so i think it has to be an 2d array or something... Really stuck here... Thanks...
10
by: Ian Todd | last post by:
Hi, I am trying to read in a list of data from a file. Each line has a string in its first column. This is what i want to read. I could start by saying char to read in 1000 lines to the array( i...
2
by: Steve | last post by:
I want an initializer for an array of pointers to arrays of strings. So I can do something like this: const char *t1 = { "a", "b", "c", NULL }; const char *t2 = { "p", "q", NULL }; const char...
1
by: Jeff | last post by:
I am struggling with the following How do I marshal/access a pointer to an array of strings within a structure Than Jef ----------------------------------------------------------------
7
by: Bob Rock | last post by:
Hello, converting from the managed to the unmanaged world (and viceversa strings) and byte arrays is something I do often and I'd like to identify the most correct and efficient way to do it....
6
by: Broeisi | last post by:
Hello, I wrote the tiny progam below just to understand arrays and strings better. I have 2 questions about arrays and strings in C. 1. Why is it that when you want to assign a string to an...
23
by: arnuld | last post by:
i was doing exercise 4.3.1 - 4.29 of "C++ Primer 4/e" where authors, with "run-time shown", claim that C++ Library strings are faster than C-style character strings. i wrote the same programme in...
19
by: bowlderyu | last post by:
Hello, all. If a struct contains a character strings, there are two methods to define the struct, one by character array, another by character pointer. E.g, //Program for struct includeing...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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
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
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...
0
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...
0
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,...

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.