Defining an array of structures in C? - c

main.h
#define DATA struct data
DATA
{
int id;
char data;
}
main.c
DATA *listOfData[100];
So at this point I will/should be able to access DATA in the list like this:
printf(listOfData[5]->data);
It isn't letting me do it, the run just freezes on that last print f...no error or anything.

This is because you have defined an array of pointers. But you never initialized any of the pointers.
Therefore:
printf(listOfData[5]->data);
will crash (undefined behavior) because you are dereferencing the (invalid) pointer at index 5.
*(And that's a very odd way to define a struct...)
To fix this issue, you will need to allocate for each of the pointers in the array. If you don't actually need it to be an array of pointers, then it might be better to just make it array of the struct itself:
DATA listOfData[100];
and access it as:
listOfData[5].data
Then you don't have to deal with allocating each element.

I'm not sure why printf would just freeze, but there's a couple of things wrong with this. First, all the pointers in your DATA* array are uninitialized. You probably intended to make an array of DATA, instead of an array of DATA pointers:
DATA listOfData[100];
You also didn't end the struct with a semicolon, so it seems unlikely that this would even compile:
#define DATA struct data
DATA
{
int id;
char data;
};
Finally, you're using printf in a rather unsafe way; the first argument needs to be a format string, or you can get weird behavior if the first argument has a % in it:
printf("%c\n", listOfData[5].data);

You haven't shown any memory allocation for the DATA *. Either declare your array as an array of struct data, like:
DATA listOfData[100];
or allocate memory dynamically and assign the pointers in your array.

Related

Trouble with structs and strings

I made a linked list whose nodes hold 4 string values. When I call the newNode function, an error says 'Node has no member named familyName', as well as the other members. My code:
I am really confused with how strings work in structs.
Your immediate problem is the type definition. You cannot call malloc() from within there, all you can do is define the fields. The memory allocation must come later. So, it should be:
typedef struct node{
char *familyName;
char *firstName;
char *address;
char *phoneNumber;
struct node *link;
}Node;
You'll strike another problem (run-time rather than compile-time) once you fix that. When you do something like:
p -> familyName = famName;
that simply copies the pointer into your structure, and the pointer is always the memory location of familyName in main().
That means every node will point to the same memory, and you're continuously updating that memory.
You won't notice the problem with the code as it stands, since you're only asking for one record. But it will become an issue when you start looping to get more records.
Your best bet is to use something like strdup() to make a copy of the string passed in, then each node will have its own memory location for strings:
p -> familyName = strdup (famName);
(don't forget to also free() the memory for each field once you're finished with it).
In the unlikely event your C implementation doesn't have a strdup(), see here.
There are several problems:-
It is not allowed to allocate memory when declaring a structure. Either, do the malloc inside your newNode() method Or, declare the structure like char familyName[50].
Result of malloc should not be casted.
It is better to use strcpy (or strdup) when copying strings

C structs sharing common pointer?

I'm currently having an issue with the following struct:
typedef struct __attribute__((__packed__)) rungInput{
operation inputOperation;
inputType type;
char* name;
char numeroInput;
u8 is_not;
} rungInput;
I create multiple structs like above inside a for loop, and then fill in their fields according to my program logic:
while (a < 5){
rungInput input;
(...)
Then when I'm done filling the struct's fields appropriately, I then attempt to copy the completed struct to an array as such:
rungArray[a] = input; //memcpy here instead?
And then I iterate again through my loop. I'm having a problem where my structs seem to all have their name value be the same, despite clearly having gone through different segments of code and assigning different values to that field for every loop iteration.
For example, if I have three structs with the following names: "SW1" "SW2" SW3", after I am done adding them to my array I seem to have all three structs point me to the value "SW3" instead. Does this mean I should call malloc() to allocate manually each pointer inside each struct to ensure that I do not have multiple structs that point to the same value or am I doing something else wrong?
When you write rungArray[i] = input;, you are copying the pointer that is in the input structure into the rungArray[i] structure. If you subsequently overwrite the data that the input structure is pointing at, then you also overwrite the data that the rungArray[i] structure is pointing at. Using memcpy() instead of assignment won't change this at all.
There are a variety of ways around this. The simplest is to change the structure so that you allocate a big enough array in the structure to hold the name:
enum { MAX_NAME_SIZE = 32 };
…
char name[MAX_NAME_SIZE];
…
However, if the extreme size of a name is large but the average size is small, then this may waste too much space. In that case, you continue using a char *, but you do indeed have to modify the copying process to duplicate the string with dynamically allocated memory:
rungArray[i] = input;
rungArray[i].name = strdup(input.name);
Remember to free the memory when you discard the rungArray. Yes, this code copies the pointer and then overwrites it, but it is more resilient to change because all the fields are copied, even if you add some extra (non-pointer) fields, and then the pointer fields are handled specially. If you write the assignments to each member in turn, you have to remember to track all the places where you do this (that would be a single assignment function, wouldn't it?) and add the new assignments there. With the code shown, that mostly happens automatically.
You should malloc memory for your struct and then store the pointers to the structs inside your array. You could also turn your structs into a linked list by adding a pointer to each struct that points to the next instance of your struct.
http://www.cprogramming.com/tutorial/c/lesson15.html

C struct malloc & array of pointers implementation

I just started programming in C a few days ago. I am now trying to learn structs.
I have this program and I want to improve it so that my array people is now an array of pointers to structs. I am not sure how to do this.
I also want to modify my insert method, to call malloc to create a new struct and set the correct array element pointing to it.
As far as I know, malloc is dinamic memory allocation but although I've read some guides I'm still unsure on how exactly to use it. Also, after using malloc, what else do I need to change in my program for it to work as before?
If you want people to be an array of pointers, you have to declare it like this:
struct person *people[12];
Remember that declaration follows use and that dereferencing has lower precedence than array indexing; this means that *people[i] is of type struct person, and thus, people[i] is a pointer to struct person.
To initialize each position in people, you call malloc() to make your pointers point to a valid memory location large enough to hold a struct person. It is as easy as:
people[i] = malloc(sizeof(struct person));
When you don't need people anymore, you have to remember to free every memory position you allocated, by calling free(people[i]) for every position i.
I noticed you declared the array to hold 12 structs. This can be dangerous when someone changes the code: it will not work when HOW_MANY is greater than 12. You should declare an array of the same size:
struct person *people[HOW_MANY];
This ensures that your array always has exactly the space needed.
UPDATE:
You need to declare insert as receiving an array of pointers instead of an array of structures:
static void insert (struct person *people[], char *name, int age) {
... }
And people[i].name is invalid. Since people[i] is a pointer now, you need to do it like this:
people[i]->name
Or, equivalently, (*people[i]).name.
The same applies to people[i]->age. Remember to change this both in main() and inside insert.
Also, consider passing i to insert instead of using static variables, unless you have a very good reason to do so. Static variables are used for functions with internal state, and for me, insert is not quite the type of function where you'd want that.

C - passing elements from within structures to functions

I have a structure comprised of an array of pointers to other structures of a different type.
typedef struct{
NodeT* nodes[2];
int size;
}stackT;
with:
typedef struct{
char info;
}NodeT;
And I have a pointer to the above (first) structure:
stackT* stackPtr;
Assuming the memory is allocated for the stackT structure as well as both NodeT structures with associated assignments for the members of both NodeT structures, how would I pass to a function one of the pointers in stackT?
For example:
void setChar(NodeT* nodePtr, char setTo){
nodePtr->info = setTo;
}
called with line:
setChar(stackPtr->nodes[0], 'A');
Does not work. I figured it had something to do with the -> syntax dereferencing the pointer whereby I am actually passing in a structure. I do not get any compilation errors, but when I check for the assignment by printing whatever is stored in char info I do not get anything.
Is the notation incorrect or do I have issues elsewhere in the program perhaps? I just wanted to rule this out first (proper passing syntax).
If someone is looking for a solution to this problem, the above code was actually correct.
Assuming you have an array of pointer in some structure and you wish to pass such a pointer, the correct syntax would be:
someFunc( structPtr->ptrArray[0] )
The line:
structPtr->ptrArray[0]
Actually returns a pointer, and not whatever the pointer is pointer to.
(*structPtr).ptrArray[0]
Is also equivalent.
That being said, I either mistakenly interpreted the information before me, or I had underlying errors elsewhere in the my code.

C - transferring information to a struct (specifically an array of strings)

This seems to be a very simple problem but I can't quite figure out which part is causing it. Basically, I have a struct that just contains an array of strings
struct command_stream{
char **tokens;
};
typedef struct command_stream *command_stream_t;
command_stream_t test;
Then later on, I parse some strings into shorter ones and end up with another array of strings
char **words = *array of strings*
words contains the correct information I want, I looped through and printed out each element to make sure I wasn't getting a faulty string. So now I just point tokens to words
test->tokens = words;
But it gives me a segmentation fault. I'm not sure why though. They're both pointers, so unless I'm missing something obvious...
EDIT: The function as a whole has to return a pointer, which is why it was set up like this, which I keep forgetting. But I think I've got it, if I just create a new typedef
typedef struct command_stream command_stream_s;
command_stream_s new_command_stream;
and just return
&new_command_stream;
That should work right? Even though new_command_stream itself isn't a pointer.
From your code excerpt, it seems that you have not declared the struct. You have successfully declared a pointer to the struct command_stream_t test; but this pointer does not point to anywhere yet.
You need to allocate memory for your struct in some way and make test reference it. For instance:
command_stream_t test =
(command_stream_t) malloc(sizeof(struct command_stream));
This way you can successfully use:
test->tokens = words;
as you intended.
Note that you don't need to use malloc to allocate the memory. The pointer can reference a local/global variable as long as it has memory associated to it (N.B. if you use a local var don't use the pointer outside the declaration scope of that var).
typedef struct command_stream *command_stream_t;
command_stream_t test;
This makes "test" a pointer. There is no memory allocated for the structure.
You need to allocate memory for the structure and make the test pointer point to the block of memory before you can dereference by saying -
test->tokens = words;
Do this:
typedef struct command_stream command_stream_t;
command_stream_t test;
test.tokens = words;
The difference is that, command_stream_t is no more a pointer type, it is the actual structure.

Resources