A question about arrays,pointers and structures
Author |
Message |
Ami #1 / 7
|
 A question about arrays,pointers and structures
Hey! I have a problem concerning both design and implementation. I have created a structure i.e. typedef struct{ ... Quote: } Job;
I need to have an array of these objects, but I do not know the number until the program is run. This suggests dynamic allocation to me. However, I'm getting a bit confused as to whether the array needs to contain pointers to Jobs or something else. I've read the FAQ with no success. I think I'm getting myself in a twist. I also wrote code that seemed to work, but then spat the dummy - so I think I've gotten confused with how this should actually be implemented. Could anybody please help? Many thanks for any help - amie
|
Sun, 21 Aug 2005 15:09:18 GMT |
|
 |
Zoran Cutur #2 / 7
|
 A question about arrays,pointers and structures
Quote: > Hey! > I have a problem concerning both design and implementation. > I have created a structure i.e. > typedef struct{ > ... > } Job; > I need to have an array of these objects, but I do not know the number > until the program is run. This suggests dynamic allocation to me. > However, I'm getting a bit confused as to whether the array needs to > contain pointers to Jobs or something else.
No, the array would simply hold objects of type Job. The Pointer you'll use will be pointing to the beginning of the array and you better don't change this pointer at anytime, because you'll lose your reference to the allocated memory and thereby introduce a memory leak. You may think of Job *x = malloc(10 * sizeof Job); as being equivalent to: Job x[10]; they are not the same, for example sizeof x will return different values, but the logical appearance is the same. Quote: > I've read the FAQ with no success. I think I'm getting myself in a > twist. I also wrote code that seemed to work, but then spat the dummy > - so I think I've gotten confused with how this should actually be > implemented.
You don't want to show us the code you have? --
"LISP is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will make you a better programmer for the rest of your days." -- Eric S. Raymond
|
Sun, 21 Aug 2005 16:32:50 GMT |
|
 |
King W.Wa #3 / 7
|
 A question about arrays,pointers and structures
Quote: > typedef struct{ > ... > } Job; > I need to have an array of these objects, but I do not know the number > until the program is run. This suggests dynamic allocation to me. > However, I'm getting a bit confused as to whether the array needs to > contain pointers to Jobs or something else.
I've never used this technique. But I think you must use: Job *jobpt; ...... jobpt = (Job *)malloc(100 * sizeof(Job)); I prefer to use: struct Job { ...... Quote: };
struct Job *jobpt; ... jobpt = (struct Job *)malloc(100 * sizeof(struct Job)); Weichao
|
Sun, 21 Aug 2005 22:03:59 GMT |
|
 |
bd #4 / 7
|
 A question about arrays,pointers and structures
Quote:
> Hey! > I have a problem concerning both design and implementation. > I have created a structure i.e. > typedef struct{ > ... > } Job; > I need to have an array of these objects, but I do not know the number > until the program is run. This suggests dynamic allocation to me. > However, I'm getting a bit confused as to whether the array needs to > contain pointers to Jobs or something else. > I've read the FAQ with no success. I think I'm getting myself in a > twist. I also wrote code that seemed to work, but then spat the dummy > - so I think I've gotten confused with how this should actually be > implemented. > Could anybody please help? > Many thanks for any help > - amie
You mean like this? Job *jobs; /* later... */ void allocatejobs(int n){ if(!jobs = malloc(sizeof *jobs * n)){ fprintf(stderr, "Error mallocing %d jobs.\n", n); exit(1); } Quote: }
/* later... */ allocatejobs(n); jobs[somenumber].someelement = foo; /* Somenumber must be less than n and greater then or equal to 0 */ -- Freenet distribution not available Help Microsoft stamp out piracy. Give Linux to a friend today! -- From a Slashdot.org post
|
Sun, 21 Aug 2005 23:19:12 GMT |
|
 |
Stefano #5 / 7
|
 A question about arrays,pointers and structures
Hi, I think that for solving your problem there are two different ways. You wrote that you are not able to determine the number of elements until the program runs. What does it mean? Does it mean that, for example, the user must enter which indicates that number in some way? Does it mean that the program is going to read a file in which the first line, for example, contains that number? If you are in situations like these, the use of an array is the better way: you must allocate it dynamically (pay attention to the pointer cast!) but afterwards it is very simple to use. A code example can be: #include <stdio.h> #include <stdlib.h> typedef struct { ... Quote: } Job;
int main (void) { int n; Job *list; printf ("Number of elements: "); scanf ("%d",&n); list = (Job *) calloc (n, sizeof(Job)); return 0; Quote: }
The other case is the one in which you are not able to determine the number of elements in any way: it appens, for example, when you read a data file with a not specified number of lines. In this case, the better data structure are named "linked lists": in every object you must have a pointer to the following structure of the list. This case is a little more complicated because the operation on the list need the use of pointers... I hope to have helped you! Stefano B.
|
Sun, 21 Aug 2005 23:57:25 GMT |
|
 |
ma #6 / 7
|
 A question about arrays,pointers and structures
Quote:
> > typedef struct{ > > ... > > } Job; > > I need to have an array of these objects, but I do not know the number > > until the program is run. This suggests dynamic allocation to me. > > However, I'm getting a bit confused as to whether the array needs to > > contain pointers to Jobs or something else. > I've never used this technique. But I think you must use: > Job *jobpt; > ...... > jobpt = (Job *)malloc(100 * sizeof(Job)); > I prefer to use: > struct Job { > ...... > }; > struct Job *jobpt; > ... > jobpt = (struct Job *)malloc(100 * sizeof(struct Job)); > Weichao
what's the difference here? (apart from using the typdef? - or is that your point?)
|
Mon, 22 Aug 2005 03:22:00 GMT |
|
 |
Barry Schwar #7 / 7
|
 A question about arrays,pointers and structures
Quote: >Hi, >I think that for solving your problem there are two different ways. >You wrote that you are not able to determine the number of elements >until the program runs. What does it mean? Does it mean that, for >example, the user must enter which indicates that number in some way? >Does it mean that the program is going to read a file in which the >first line, for example, contains that number? >If you are in situations like these, the use of an array is the better >way: you must allocate it dynamically (pay attention to the pointer >cast!) but afterwards it is very simple to use. >A code example can be: >#include <stdio.h> >#include <stdlib.h> >typedef struct { > ... >} Job; >int main (void) >{ > int n; > Job *list; > printf ("Number of elements: "); > scanf ("%d",&n); > list = (Job *) calloc (n, sizeof(Job));
Actually, you should omit the cast. Its presence can never help but can hurt. For example, if you forget to include stdlib.h, there is no prototype for calloc. Under the C89 standard, the compiler is obliged to assume that calloc returns int. Without the cast, the compiler is required to produce a diagnostic informing you that you are not allowed to assign an int to a pointer. This is your clue to remember stdlib.h With the cast, this diagnostic is suppressed. Since there are no diagnostics, you assume your code is reasonably correct. If your system treats ints and pointers differently, you could be invoking undefined behavior. Quote: > return 0; >}
<<Remove the del for email>>
|
Tue, 23 Aug 2005 10:45:59 GMT |
|
|
|