473,386 Members | 1,610 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,386 software developers and data experts.

Asigning to an array of strings??

Hey guys, im trying to assign a read-in string to a string in an array
(Right at the bottom of the post). However the program crashes when it gets
to this line.. Obviously it's somthing to do with the way i am accessing the
array or writing to it.. But im not exactly sure what.. ??

I've given all the code you should need to understand the problem, in the
clearest possible way, below.

Structures:

struct employee { /* Create the employee structure */
int pid;
char name[4];
char job[40];
int hourlyRate;
int rest;
char *appointments[7];
struct employee *next;
};

Typedefs:

typedef struct employee PERSON;
typedef PERSON *PTR;

And I have a loop in a function calling:

new = (PTR)malloc(sizeof(PERSON)); /* NEW is a PTR as is HEAD */
new->next = head; /* Make the new node link to the first node. */
head = new; /* Make head now point to the new element. */
new->pid = store.pid; /* Fill values with data. */
strncpy(new->name, store.name, 3);
strncpy(new->job, store.job, 40);
new->hourlyRate = store.hourlyRate;
new->rest = (store.rest - 1);
/* THIS LOOP HERE */
for(i = 0; i < 7; i++) {
strcpy(new->appointments[i], "No booking");
if(i = new->rest) {
strcpy(new->appointments[i], "BREAK PERIOD");
}
/* THIS LOOP HERE */
}
Nov 14 '05 #1
6 1496
Simon Mansfield <gl************@ntlworld.com> wrote:
Hey guys, im trying to assign a read-in string to a string in an array
(Right at the bottom of the post). However the program crashes when it gets
to this line.. Obviously it's somthing to do with the way i am accessing the
array or writing to it.. But im not exactly sure what.. ?? I've given all the code you should need to understand the problem, in the
clearest possible way, below. Structures: struct employee { /* Create the employee structure */
int pid;
char name[4];
char job[40];
int hourlyRate;
int rest;
char *appointments[7];
struct employee *next;
}; Typedefs: typedef struct employee PERSON;
typedef PERSON *PTR;
If you really typedef pointers to typedef'ed structures (which many
people here recommend strongly against) then at least give it a
reasonable name like PERSON_PTR or similar.
And I have a loop in a function calling: new = (PTR)malloc(sizeof(PERSON)); /* NEW is a PTR as is HEAD */
Don't cast the return value of malloc() - it doesn't buy you anything
and keeps the compiler from warning you if you forget to include
<stdlib.h>
new->next = head; /* Make the new node link to the first node. */
head = new; /* Make head now point to the new element. */
new->pid = store.pid; /* Fill values with data. */
strncpy(new->name, store.name, 3);
Are you sure you don't want a '4' as the last argument here?
strncpy(new->job, store.job, 40);
When you use strncpy() you must be careful that the resulting string has
a terminating '\0' - if the source string was too long the destination
string won't have one and trying to use it as a string will fail badly.
new->hourlyRate = store.hourlyRate;
new->rest = (store.rest - 1);
/* THIS LOOP HERE */
for(i = 0; i < 7; i++) {
strcpy(new->appointments[i], "No booking");


new->appointments is an array of 7 char pointers that point to random
places in memory. You first have to assign to them pointers to memory
you own before you can use them. The only exception is the case that
you only will use them to store the addresses of literal strings, in
which case you could use

new->appointments[ i ] = "No booking";

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2
<Je***********@physik.fu-berlin.de> wrote in message
news:31*************@uni-berlin.de...
Simon Mansfield <gl************@ntlworld.com> wrote:
Hey guys, im trying to assign a read-in string to a string in an array
(Right at the bottom of the post). However the program crashes when it
gets
to this line.. Obviously it's somthing to do with the way i am accessing
the
array or writing to it.. But im not exactly sure what.. ??

I've given all the code you should need to understand the problem, in the
clearest possible way, below.

Structures:

struct employee { /* Create the employee structure */
int pid;
char name[4];
char job[40];
int hourlyRate;
int rest;
char *appointments[7];
struct employee *next;
};

Typedefs:

typedef struct employee PERSON;
typedef PERSON *PTR;


If you really typedef pointers to typedef'ed structures (which many
people here recommend strongly against) then at least give it a
reasonable name like PERSON_PTR or similar.
And I have a loop in a function calling:

new = (PTR)malloc(sizeof(PERSON)); /* NEW is a PTR as is HEAD */


Don't cast the return value of malloc() - it doesn't buy you anything
and keeps the compiler from warning you if you forget to include
<stdlib.h>
new->next = head; /* Make the new node link to the first node. */
head = new; /* Make head now point to the new element. */
new->pid = store.pid; /* Fill values with data. */
strncpy(new->name, store.name, 3);


Are you sure you don't want a '4' as the last argument here?
strncpy(new->job, store.job, 40);


When you use strncpy() you must be careful that the resulting string has
a terminating '\0' - if the source string was too long the destination
string won't have one and trying to use it as a string will fail badly.
new->hourlyRate = store.hourlyRate;
new->rest = (store.rest - 1);
/* THIS LOOP HERE */
for(i = 0; i < 7; i++) {
strcpy(new->appointments[i], "No booking");


new->appointments is an array of 7 char pointers that point to random
places in memory. You first have to assign to them pointers to memory
you own before you can use them. The only exception is the case that
you only will use them to store the addresses of literal strings, in
which case you could use

new->appointments[ i ] = "No booking";

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de


I have now implemented everything you suggested, so thanks a bunch!!

Also (you may have seen my previous post) Im trying to read in a file like:

34:JVT:Crimp and Perm:435:2
65:NAH:Manicurist:514:4
21:CJW:Non Surgical Facelifts:1500:5
78:MWJ:Pedicures:310:1
7:PWG:Ear Piercing:510:7
15:JAS:Leg Waxing:300:4
96:AMG:Bikini Waxing:451:3

I'm currently using the command (Sorry it wont fit on one line's!):

fscanf(input, "%d : %s : %[^:] %d : %d", &store.pid, store.name, store.job,
&store.hourlyRate, &store.rest);

But it doesnt seem to store the values that I expect.. Any idea why that
is??
Nov 14 '05 #3
Simon Mansfield <gl************@ntlworld.com> wrote:
Structures:
struct employee { /* Create the employee structure */
int pid;
char name[4];
char job[40];
int hourlyRate;
int rest;
char *appointments[7];
struct employee *next;
};

Also (you may have seen my previous post) Im trying to read in a file like:

34:JVT:Crimp and Perm:435:2
65:NAH:Manicurist:514:4
21:CJW:Non Surgical Facelifts:1500:5
78:MWJ:Pedicures:310:1
7:PWG:Ear Piercing:510:7
15:JAS:Leg Waxing:300:4
96:AMG:Bikini Waxing:451:3 I'm currently using the command (Sorry it wont fit on one line's!): fscanf(input, "%d : %s : %[^:] %d : %d", &store.pid, store.name, store.job,
&store.hourlyRate, &store.rest); But it doesnt seem to store the values that I expect.. Any idea why that
is??


Don't put unnecessary spaces into the format string - fscanf() will have
to match them. And you're missing a ':' after the second string. For
safety reasons you also should specify the maximum number of chars you
are prepared to store in a string, i.e.

fscanf( input, "%d:%3s:%39[^:]:%d:%d", &store.pid, store.name, store.job,
&store.hourlyRate, &store.rest);

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #4

<Je***********@physik.fu-berlin.de> wrote in message
news:31*************@uni-berlin.de...
Simon Mansfield <gl************@ntlworld.com> wrote:

Structures:

struct employee { /* Create the employee structure */
int pid;
char name[4];
char job[40];
int hourlyRate;
int rest;
char *appointments[7];
struct employee *next;
};

Also (you may have seen my previous post) Im trying to read in a file
like:

34:JVT:Crimp and Perm:435:2
65:NAH:Manicurist:514:4
21:CJW:Non Surgical Facelifts:1500:5
78:MWJ:Pedicures:310:1
7:PWG:Ear Piercing:510:7
15:JAS:Leg Waxing:300:4
96:AMG:Bikini Waxing:451:3

I'm currently using the command (Sorry it wont fit on one line's!):

fscanf(input, "%d : %s : %[^:] %d : %d", &store.pid, store.name,
store.job,
&store.hourlyRate, &store.rest);

But it doesnt seem to store the values that I expect.. Any idea why that
is??


Don't put unnecessary spaces into the format string - fscanf() will have
to match them. And you're missing a ':' after the second string. For
safety reasons you also should specify the maximum number of chars you
are prepared to store in a string, i.e.

fscanf( input, "%d:%3s:%39[^:]:%d:%d", &store.pid, store.name, store.job,
&store.hourlyRate, &store.rest);

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de

Thanks dude... works a treat now ;)

Simon
Nov 14 '05 #5
In article <31*************@uni-berlin.de>
<Je***********@physik.fu-berlin.de> wrote:
Don't put unnecessary spaces into the format string - fscanf() will have
to match them.
Well, yes; but note that space directives match *any* amount of
*any kind* of whitespace, from none, to a single blank, to 198
newlines in a row.
And you're missing a ':' after the second string.


(This would be the one causing the actual problem.)

It is still best for most people to avoid the scanf() family,
except for occasional calls to sscanf().
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #6
Chris Torek <no****@torek.net> wrote:
In article <31*************@uni-berlin.de>
<Je***********@physik.fu-berlin.de> wrote:
Don't put unnecessary spaces into the format string - fscanf() will have
to match them.
Well, yes; but note that space directives match *any* amount of
*any kind* of whitespace, from none, to a single blank, to 198
newlines in a row.


Thank you for the clarification (I actually wasn't aware that
a space is even matched by *no* space in the input, as I have
to admit...).
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #7

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

Similar topics

7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
3
by: SilverWolf | last post by:
I need some help with sorting and shuffling array of strings. I can't seem to get qsort working, and I don't even know how to start to shuffle the array. Here is what I have for now: #include...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
7
by: arkobose | last post by:
hey everyone! i have this little problem. consider the following declaration: char *array = {"wilson", "string of any size", "etc", "input"}; this is a common data structure used to store...
12
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
11
by: Bob Rock | last post by:
Hello, I have an array of strings and need to find the matching one with the fastest possible code. I decided to order the array and then write a binary search algo. What I came up with is the...
2
by: zensunni | last post by:
I'd like to assign an array to a cookie. But, I can't incrementally assign cookie names, thus, I can't assign different values. Here is the code that should work, but doesn't let me. count = 0...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.