i have a global array of char pointers declared as:

char *strings[ARRAY_SIZE];

I don't seem to understand the arithmetic necessary to compare individual characters in each array element. I haven't had any luck finding concise examples. Maybe someone can point me in the right direction?

Recommended Answers

All 5 Replies

'In'? You're probably thinking the wrong way. Each array element contains the address of a bunch of characters. So strings[3][0] == strings[4][0] compares two characters: the character located at the memory address computed by the expression, 'strings[3]', and the character located at the memory address computed by 'strings[4]'.

I just phrased myself incorrectly. Each element is pointing to the start of a string. I'm trying to understand the arithmetic involved in accessing an individual character from one of those strings.

I just phrased myself incorrectly. Each element is pointing to the start of a string. I'm trying to understand the arithmetic involved in accessing an individual character from one of those strings.

Okay. The trick is to remember that in C, an expression x[y] is just a cute way to write *(x + y) .

Seriously, writing x[3] is equivalent to 3[x] . Maybe C++ is a bit more strict.

So, applying C's order of precedence, strings[4][2] is equivalent to (strings[4])[2] , which is (*(strings + 4))[2] , which is *(*(strings + 4) + 2) . So, strings + 4 is the location of a memory address. Then *(strings + 4) is the memory address of the first character of an array of characters. Add two to that, and you get the memory address of the third character. Then dereference that, and you've got the character itself.

Arrays are a block of contiguous memory with enough memory to hold a known number of elements. Each element must be of the same type and will therefore take up the same amount of memory. So if I have an element that will take 1 byte of memory then an array of 10 elements of that type will take 10 bytes of contiguous memory; and if I have an element that takes 4 bytes of memory and I have an array of 10 of those elements then the array will need 40 bytes of contiguous memory.

Now lets say I have a pointer that contains an address that corresponds to the first element of a known array of elements of known size. To obtain the address for the first element in the array I could do this:

ptr + (0 * sizeof(element))

and the address of the third element would be

ptr + (2 * sizeof(element))

and the address of an arbritrary element x would be

ptr + ((x - 1) * sizeof(element ))

To find out what's at the memory address we'd have to derefernce it like this:

*(ptr + ((x - 1) * sizeof(element))

But that's clunky so let's say the name of an array is the address of the first element of the array and [x - 1] is equivalent to ((x - 1) * sizeof(element)). Then we can say that

arrayName[x - 1] is the same as *(ptr + ((x - 1) * sizeof(element))

or, another way of looking at it is that the address of the element with index x is x * sizeof(element) bytes away from the first address of the array. (N.B. This is why array indexes are zero based and not 1 based.)

Now lets say that all memory addresses require 4 bytes of memory. Then a pointer would have memory size of 4 bytes. And an array of 10 pointers would require 40 bytes. If you wanted to find the address at
ptrs[3] it would be ptrs + 12 bytes.

Now let's say that each pointer points to an array of 10 char. Each char has 1 byte of memory. You want to find the address of the 2nd char in the 3rd array of char pointed to by the array of pointers to char. Then the address of the first element of the 3rd char array in the array of pointer to char array would be

arrayOfPointerName + ((3 - 1) * 4)

and the address of the 2nd element of the char array starting at that address would be

(arrayOfPointerName + ((3-1) * 4) ) + ((2 - 1) * 1)

And the value at that address would be

*((arrayOfPointerName + ((3-1) * 4) ) + ((2 - 1) * 1))

Yuck. Much easier to say the value of the 2nd char of the 3 string is

arrayOfPointerName[2][1];

And to compare equivalency of two char from the arrayOfPointerName would be

if(arrayOfPointerName[x][y] == arrayOfPointerName[a])

etc. That makes it much easier for us to read but the computer probably does all the calculations involved with

*((arrayOfPointerName + ((3-1) * 4) ) + ((2 - 1) * 1))

because those are all numbers and they can be manipulated very very quickly.

Now, if you're like me, you'll need to kick back and let your brain relax for a minute before you move on to something else.

Thanks Lerner & Rashakil Fol. I understand the syntax now. I just didn't realise that yu could saze time by applying two-dimensional array sytax to achieve that purpose. Both of your explanations were detailed and concise. Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.