I am a mid-level programmer, working to learn Standard C. I’m currently struggling through a class exercise which involves using pointers to store different kinds of data types into an array of type char.
Suppose I have a large char array:
static char arr[1000];
As my professor explained it, I can consider this a chunk of local memory, where each element in the array has a granularity of one byte. That seems useful. Now suppose I want to take the first four bytes/elements and store an int:
int a = 100;
int* ptr = (int*)arr;
*ptr = a;
As I understand it, the second line creates an int* pointer, and then points it at the beginning of array arr. The third line writes the value of a into that location. Because ptr is a pointer of type int and because arr has plenty of space, this write four bytes / four element’s worth of data because sizeof(int) == 4. Watching this carefully through my debugger seems to confirm this.
So far, so good. Now let's say I wanted to expand this concept. Let’s say I wanted to store the following into my array, in this order:
int a = 100;
int b = 200;
char* str = “My dog has fleas”
int c = 300;
Which would logically look like this:
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
--------------------------------------------------------------------------------------
[ 100 ] [ 200 ] M y d o g h a s f l e a s \0 [ 300 ]
I need to be able to store data into the array in this manner, and then later, knowing the array structure in advance, be able to read the array. Below is my code & output, sorry in advance for the long length. It compiles but does not work. Scrutinizing it with my debugger has been very confusing; I can't tell where (and how often) I'm going off-track. If anyone has any insight or advice, I will be very grateful.
int main(){
static char arr[1000];
int a = 100;
int b = 200;
char* str = "My dog has fleas";
int c = 300;
// Create pointers to load data:
int* ptrA = arr; // points to start of array
int* ptrB = ptrA + sizeof(int); // points 4 bytes into array
char* ptrStr = ptrB + sizeof(int); // points 8 bytes into array
int* ptrC = ptrStr + sizeof("My dog has fleas"); // points to after the string
// (I don't know how to use sizeof() to measure the actual length of the string
// Load data into my array
*ptrA = a; // Assign int 100 into the array?
*ptrB = b; // Assign int 200 into the array?
*ptrStr = memcpy(ptrStr, str, sizeof("My dog has fleas")); // Write "My dog has fleas" into the array?
*ptrC = c; // Assign int 300 into the array?
// Knowing the array's structure, walk it and print results:
char* walkIt = arr;
int counter = 0;
while (counter < 30) {
if (counter == 0) {
// we are pointing at what should be the first int
int* tmpPtr1 = (int*)walkIt;
printf("%d ", *tmpPtr1);
}
else if (counter == 4) {
// we are pointing at what should be the second int
int* tmpPtr2 = (int*)walkIt;
printf("%d ", *tmpPtr2);
}
else if (counter == 8) {
// we are pointing at what should be the string
printf("%s ", walkIt);
}
else if (counter == 25) {
// we are pointing at what should be the third int
int* tmpPtr3 = (int*)walkIt;
printf("%d ", *tmpPtr3);
}
walkIt++; // Continue walking the array
counter++; // Don't walk too far
}
return 0;
}
Output is this:
100 0 0