Problem with pointer arithmetics

Hi there,

I have have some trouble concerning pointers.

Problem:
I have a struct
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct elementtype{
	int			elementcount;
	char			*str1;
	char			*str2;
	char			*str3;
	char			*str4;
	char			*str5;
	char			*str6;
	char			*str7;
	char			*str8;
	char			*str9;
	char			*str10;
};

typedef struct elementtype elementtype;
which is used as storing element of a linked list.

Till now I access the elements of these struct by "hand".
e.g. for initializing all members of one such struct:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
elementtype InitializeElement(elementtype el, int size){

	el.elementcount = 0;
	el.str1 = (char*)calloc(size, sizeof(char));
	el.str2 = (char*)calloc(size, sizeof(char));
	el.str3 = (char*)calloc(size, sizeof(char));
	el.str4 = (char*)calloc(size, sizeof(char));
	el.str5 = (char*)calloc(size, sizeof(char));
	el.str6 = (char*)calloc(size, sizeof(char));
	el.str7 = (char*)calloc(size, sizeof(char));
	el.str8 = (char*)calloc(size, sizeof(char));
	el.str9 = (char*)calloc(size, sizeof(char));
	el.str10 = (char*)calloc(size, sizeof(char));

	return el;
}


I find this rather inconvenient, 'cause when I change the count of elements in the struct, I have to rework every function working with the elements seperatly.

Now to my question;

Is there a way to make functions like InitializeElement() more dynamicly?
When I make statement like elementtype element in my code, element gets a starting address, e.g. 0xffffff00.
So the address of elementcount should be the same (0xffffff00) and the address of the next element of this struct (.str1) should be at address 0xffffff04.

Is there anyway of just getting the starting address of a variable of this structtype and incrementing this address to access its members?

Right now im reading a textline
"arg1;arg2;arg3;arg4;...;arg10"
into element.str1.
Then i want to split it into tokens with delimiter ';' and copy each token at element.str# (arg2 at .str2, arg3 at .str3 and so on)

At the moment my code for this is very repitative
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
elementtype SplitIntoTokens(char* str_contend){

	char* token, *tmp;
	tmp = new char[LINE_LENGTH_MAX];
	memset(tmp, 0, LINE_LENGTH_MAX);
	token = new char[LINE_LENGTH_MAX];
	memset(token, 0, LINE_LENGTH_MAX);
	elementtype element = InitializeElement(element, LINE_LENGTH_MAX);
	int len = strlen(str_contend);

	strncpy(tmp, str_contend, len);

	token = strtok (tmp,";\n");
	len = strlen(token);
	strncpy(element.str1, token, len);

	token = strtok (NULL, ";\n");
	len = strlen(token);
	strncpy(element.str2,token, len);

	token = strtok (NULL, ";\n");
	len = strlen(token);
	strncpy(element.str3, token, len);

	token = strtok (NULL, ";\n");
	len = strlen(token);
	strncpy(element.str4, token, len);

	token = strtok (NULL, ";\n");
	len = strlen(token);
	strncpy(element.str5, token, len);

	token = strtok (NULL, ";\n");
	len = strlen(token);
	strncpy(element.str6, token, len);

	token = strtok (NULL, ";\n");
	len = strlen(token);
	strncpy(element.str7, token, len);

	token = strtok (NULL, ";\n");
	len = strlen(token);
	strncpy(element.str8, token, len);
	

	return element;
}


I would like to do something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
elementtype SplitIntoTokens(char* str_contend){

	char* token, *tmp;
	tmp = new char[LINE_LENGTH_MAX];
	memset(tmp, 0, LINE_LENGTH_MAX);
	token = new char[LINE_LENGTH_MAX];
	memset(token, 0, LINE_LENGTH_MAX);
	elementtype element = InitializeElement(element, LINE_LENGTH_MAX);
	int len = strlen(str_contend);

	strncpy(tmp, str_contend, len);

	token = strtok (tmp,";\n");
	len = strlen(token);
	strncpy(element.str1, token, len);

	for(int i = 0; i < 10; i++){
		token = strtok (NULL, ";\n");
		len = strlen(token);
		strncpy(#adress of element + (i * sizeof(char))#,token, len);
	}


	return element;
}
Last edited on
When I make statement like elementtype element in my code, element gets a starting address, e.g. 0xffffff00.
So the address of elementcount should be the same (0xffffff00) and the address of the next element of this struct (.str1) should be at address 0xffffff04.


I don't think that's true...it might be in most cases, but it some cases it might not be. The only thing you are guarenteed (IIRC) is that all of the elements in the array are one after each other. So therefore, you can't really do it any other way then the way you are describing I think.
As far I know on machines with 4gb (32 bit machines) or less addressable ram pointersize should be 32 bit or 4 byte.
On 64bit machines this size changed to 64bit -> 8 byte.

So, ok on 64 bit machines simply adding 4 byte to the starting address would fail, but this could be prevented by checking machine type even on runtime or during compile time.

Or am I missing some essential part here?
How come no one has suggested using an array of pointers, yet?
1
2
3
4
5
char *strs[10];
//...
for (int i=0;i<this->size;i++){
    //...
}

If I understand correctly what OP said, that's a really, really bad idea.
Well, why didn't I get this idea by myself??
Does what I want and prevents nasty pointer arithmetics.
Well thank you helios, I very gratefull.
Topic archived. No new replies allowed.