Pointer & Arrays[?]

Pointer & Arrays[?]

Screw smart intro's. Let's cut to the chase!

We declare a table M[10][10]. Ok?
And then we need the value of M[ i ][ j ], where 'i' and 'j' are obviously the line and the row of said value.

According to what was written on the blackboard during today's class, *(*(M+i)+j) = M[ i ][ j ]
Since *'something' is the value at the address 'something' [ex: *p is the value at the address stored in p], then *(M+i)+j = &M[ i ] [ j ] a.k.a. the address of M[ i ][ j ].

Good. Here I get lost:

Isn't then *(M+i) a VALUE, to which we add j, another VALUE? Then how do we get an ADDRESS?

I have some more questions, but they are hard to explain. I am waiting for some answers and we'll go from there. Perhaps solving this will also solve my other problems. This time for real, thanks in advance!
"You have enemies? Good. That means you've stood up for something, sometime in your life." --- Winston Churchill

RatkinHHK
Experienced User

Posts: 72
Joined: Tue Nov 11, 2014 6:06 pm
Blog: View Blog (0)

Re: Pointer & Arrays[?]

While I've yet to get any practical experience with pointers, I do know of the concept. A quick Google search gave me the following (in regards to C++):
cplusplus.com wrote:The address of a variable can be obtained by preceding the name of a variable with an ampersand sign (&), known as address-of operator. For example: foo = &myvar;

So, I would think getting the starting address of your table would result in the variable you're looking for. I won't speculate on how it looks in memory, as I've yet to learn about that. Maybe someone else will be able to answer more thoroughly.
Free your mind / Think clearly

cyberdrain
Expert

Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)

Re: Pointer & Arrays[?]

For a M[10][10]:
M[i][j] =*(&M[0][0]+i*10+j) = *(*(M + i) + j);

Since an array isn't actually a value, in memory it is just a address(the starting point of the array, the address is constant), A 2 dimensional array is an array of arrays,

Taking the Example above: M+i will go to the i element(which is an array) of the M, and the operator * will get the element from M, *(M + i)+j will go to the element in the sub array M+i and * will get the value of the element (*(M + i)+j).

In memory the arrays are stored like this |0|1|2|3|...|n-1|n|, depending on their data-type (char 1 byte per element, short 2 bytes per element, long 4 bytes per element, long long 8 bytes per element, Variables are stored in the Stack and Allocated Memory in the Heap).

ghost107
Poster

Posts: 316
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)

Re: Pointer & Arrays[?]

another way to think about it is to forget the two dimensional-ness entirely and imagine it as an array of pointers:

char* M[10];

so by dereferencing M, we get a pointer to a char. square brackets (when accessing) is simply dereferencing with a positional offset, so;

M[i] == *(M + i)

(although an important note is that is actually slightly incorrect because it doesn't take into account the size of the value, the real equivalency is M[i] == *(M + i * sizeof(char*)) )

now that we have a list of char pointers that we can dereference whenever we want, we give it char's to point it. actually we give it arrays of chars to introduce the second dimensional-ness again.

int i;
for (i = 0; i < 10; i++)
M[i] = new char[10];

(i believe that's how you do it, i haven't coded C in a while, sorry : ( . )

because C arrays are just slightly fancy pointers, this is perfectly valid. and now we can do the same exact thing with those arrays (pointers):

int i, j;
for (i = 0; i < 10; i++)
for (j = 0; j < 10; j++)
printf("M[%i][%i] = %c", i, j, *(*(M + i * sizeof(char*)) + j * sizeof(char)));

damn that looks ugly. that's why we use the [] operator instead of manually dereferencing arrays

Contributor

Posts: 686
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)

Re: Pointer & Arrays[?]

I was trying to fuck with some code to try and demonstrate C's array addressing yadda yadda
Code: Select all
`#include <stdio.h>int main(){char a[10] = {'1','2','3','4','5','6','7','8','9','0'};printf("%c\n", a[0][1]);}`

I can compile and run a program requesting the 15th element in a 10 element array, No problem with that. Ask for the 5th element of the first row? Well now that's some dangerous thinking, pretentious.
Goatboy wrote:Oh, that's simple. All you need to do is dedicate many years of your life to studying security.

IF you feel like exchanging ASCII arrays, let me know
Can you say brainwashing It's a non stop disco

pretentious

Posts: 1202
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)

Re: Pointer & Arrays[?]

Hmm.. It helps, but there are still things that do not make sense. So I have this idea. I have a very comprehensive C book around, so I will study about pointers from there. I will look up some information about pointer arithmetic on the internet, and basically I will try everything else to solve this. If I fail, I will ask for further clarifications. I once again took the easy road, and asked instead of doing EVERYTHING else to figure it out myself. I feel bad...
"You have enemies? Good. That means you've stood up for something, sometime in your life." --- Winston Churchill

RatkinHHK
Experienced User

Posts: 72
Joined: Tue Nov 11, 2014 6:06 pm
Blog: View Blog (0)