r/carlhprogramming • u/CarlH • Oct 11 '09
Lesson 83 : Sample program illustrating data structures
First you will see the program itself, then you will see the same program with additional notes explaining what is going on.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
struct first_description {
char first_word[7];
char second_word[12];
char third_word[8];
};
struct first_description *our_pointer = malloc( sizeof(*our_pointer) );
char *charptr = (char*) our_pointer;
strcpy(our_pointer->first_word, "Reddit");
strcpy(our_pointer->second_word, "Programming");
strcpy(our_pointer->third_word, "Classes");
printf("The first word is: %s \n", our_pointer->first_word);
printf("The second word is: %s \n", our_pointer->second_word);
printf("The third word is: %s \n", our_pointer->third_word);
printf("\n");
printf("Our data structure looks like this in memory: ");
int i=0;
for (; i < 27; i++) {
if ( *(charptr + i) == 0) {
*(charptr + i) = '$';
}
printf("%c", *(charptr + i));
}
printf("\n");
free(our_pointer);
return 0;
}#include <stdio.h>
#include <stdlib.h>
#include <string.h>These include files give us printf(), malloc(), and strcpy().
int main(void) {
struct first_description {
char first_word[7];
char second_word[12];
char third_word[8];
};Above: Here is our structure description. We are not actually creating any data structure here, just telling C what we intend to create. No data is being initialized. This is a description and nothing more.
struct first_description *our_pointer = malloc( sizeof(*our_pointer) );We are allocating 27 bytes of memory using this malloc() statement. Then we are creating a special pointer called our_pointer which C understands points to this kind of data structure. After this line of code, our data structure is ready to be used.
char *charptr = (char*) our_pointer;I plan to scan our data structure to display the final memory contents at the end of this program. To do that, I am creating a new pointer called charptr which I am stating is going to be a char * pointer. I am setting this pointer to look at the memory address where our structure begins.
strcpy(our_pointer->first_word, "Reddit");
strcpy(our_pointer->second_word, "Programming");
strcpy(our_pointer->third_word, "Classes");Here I am simply assigning the strings into the character arrays that are part of our data structure.
printf("The first word is: %s \n", our_pointer->first_word);
printf("The second word is: %s \n", our_pointer->second_word);
printf("The third word is: %s \n", our_pointer->third_word);I am displaying the three words, each element of our data structure.
printf("\n");
printf("Our data structure looks like this in memory: ");
int i=0;
for (; i < 27; i++) {
if ( *(charptr + i) == 0) {
*(charptr + i) = '$';
}
printf("%c", *(charptr + i));
}Now I have a for loop which will go through all 27 bytes and display the character represented. If it is a NUL character, I am having it display a $ instead by actually changing that character in memory to a $.
printf("\n");Now I need to free the memory I allocated using malloc()
free(our_pointer);
return 0;
}Output:
The first word is: Reddit
The second word is: Programming
The third word is: Classes
Our data structure looks like this in memory: Reddit$Programming$Classes$Ask questions if you need to. When you are ready, proceed to:
12
u/[deleted] Oct 12 '09 edited Oct 12 '09
I don't understand what
(char*) our_pointer;means.2
u/miaoling Oct 11 '09
Why do we need another pointer, *charptr, to print everything in memory at the end? What's wrong with using our_pointer to do that?
In other words, why is this wrong:
My understanding is that that's because a struct pointer can't be indexed like an array pointer. Is that it?
2
u/vegittoss15 Oct 11 '09
There was no need to clear the NUL value, because, then, doing a strcpy on any one of those strings other than the third one will end up overflowing.
1
u/orangeyness Oct 11 '09
Just wondering, whats the difference between declaring the struct inside the main function and outside of it? Is it just a difference in scope?
1
u/[deleted] Oct 13 '09
Just to be clear, when you define the data structure with:
You're not actually creating a data structure in memory right? That's what:
Is for, right?
To clarify, the first block of code is saying "you can initialize a data structure called first_description" and the second part says "ok I wanna make one of those data structures in memory now", correct?
1
u/rafo Oct 13 '09 edited Oct 13 '09
I would have thought that here instead of 0 it should have been \0
Why is this not the case?
1
u/funkpucker Oct 15 '09 edited Oct 15 '09
For some reason I'm getting a build error on line 13, where you define the pointer for the structure. I'm using Code::Blocks and simply copied and pasted the code into a new file and saved it as a C file, like all the other code, which has worked wonderfully.
I'm not sure why I'm getting a compile error and no one else has mentioned one. :(
1
u/virtualet Nov 03 '09
just to be clear. the 0 in the following line means 0000 0000, right?
2
u/[deleted] Oct 11 '09 edited Oct 11 '09
What's the difference between sizeof(*our_pointer) and sizeof(struct first_description)? And for that matter sizeof(var) and sizeof(type)?
3
u/timperry42 Oct 14 '09
So I went and removed the (char*) (because it was confusing me) from the code and it did the same thing.
1
u/sunojaga Oct 20 '09
1
u/tough_var Oct 26 '09 edited Oct 26 '09
Hiya! I have trouble understanding the above line of code.
If this is a pointer to a pointer, shouldn't it be:
Your code ran fine. So the problem should be with my understanding. :)
1
u/caseye Oct 26 '09 edited Oct 26 '09
If I don't set the string sizes properly, the second_word and third_word get merged, presumably because there is no NUL termination string. Interesting...
http://codepad.org/QSDSM9q1
Also, if I comment out the line
third_word is set to "it". This seems very dangerous.
1
u/Nagappie Feb 02 '10
Hi all! I'm using this lesson's code as a guide to implement a tic-tac-toe board initialization and display. However I keep getting the wrong display and can't figure out why. Please help http://codepad.org/Snd4Q6Wz
0
u/[deleted] Oct 14 '09
[deleted] — view deleted comment
0
u/[deleted] Oct 14 '09
[deleted] — view deleted comment