Asigning to an array of strings??

  • Thread starter Simon Mansfield
  • Start date
S

Simon Mansfield

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, "No booking");
if(i = new->rest) {
strcpy(new->appointments, "BREAK PERIOD");
}
/* THIS LOOP HERE */
}
 
J

Jens.Toerring

Simon Mansfield said:
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
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, "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
 
S

Simon Mansfield

Simon Mansfield said:
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
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, "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


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??
 
J

Jens.Toerring

Simon Mansfield said:
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
 
S

Simon Mansfield

Simon Mansfield said:
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


Thanks dude... works a treat now ;)

Simon
 
C

Chris Torek

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().
 
J

Jens.Toerring

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top