Pointer & Arrays[?]

Pointer & Arrays[?]

Post by RatkinHHK on Tue Dec 09, 2014 12:55 pm
([msg=85739]see Pointer & Arrays[?][/msg])

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
User avatar
RatkinHHK
Experienced User
Experienced User
 
Posts: 72
Joined: Tue Nov 11, 2014 6:06 pm
Blog: View Blog (0)


Re: Pointer & Arrays[?]

Post by cyberdrain on Tue Dec 09, 2014 3:14 pm
([msg=85740]see Re: Pointer & Arrays[?][/msg])

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
User avatar
cyberdrain
Expert
Expert
 
Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)


Re: Pointer & Arrays[?]

Post by ghost107 on Tue Dec 09, 2014 7:23 pm
([msg=85743]see Re: Pointer & Arrays[?][/msg])

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).
User avatar
ghost107
Poster
Poster
 
Posts: 316
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)


Re: Pointer & Arrays[?]

Post by WallShadow on Tue Dec 09, 2014 8:37 pm
([msg=85745]see Re: Pointer & Arrays[?][/msg])

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
User avatar
WallShadow
Contributor
Contributor
 
Posts: 686
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)


Re: Pointer & Arrays[?]

Post by pretentious on Wed Dec 10, 2014 6:19 am
([msg=85750]see Re: Pointer & Arrays[?][/msg])

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
User avatar
pretentious
Addict
Addict
 
Posts: 1201
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)


Re: Pointer & Arrays[?]

Post by RatkinHHK on Wed Dec 10, 2014 6:46 pm
([msg=85775]see Re: Pointer & Arrays[?][/msg])

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
User avatar
RatkinHHK
Experienced User
Experienced User
 
Posts: 72
Joined: Tue Nov 11, 2014 6:06 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests