Got trouble understanding Pointers

Hey,

I'm now learning how to use Pointers, but I'm stuck already.

int* FirstPointer = &Variable;

That will work. But when using an array, it MUST be this:
int* FirstPointer = Variable;

So no more & in the variable (array in this case) name.
So why is the & needed in normal variables and why isn't it allowed in array's?

I'm quite confused about this.

Thanks
The array name is the address of the first item in the array.

In C and C++, the code a[ n ] is a shortcut for *(a + n).

Hope this helps.
Yes, I understand that, but I still don't understand why &arrayname is wrong but &normalvarname is correct.

Thanks :)
1
2
int Variable[100];
int* FirstPointer = &Variable[0];


now you should be happy..!!??
Aaah, I understand it now.

Thanks :)
closed account (S6k9GNh0)
Same as:
1
2
3
4
5
int variable[3];
int * firstPointer = &variable;
firstPointer[2] = 3;
//And since firstPointer[0] is in the exact same location as firstPointer.
firstPointer = 6; //firstPointer[0] now equals 6 and vice versa. 

Last edited on
Ok, I think I understand it.

But now I'm stuck again.

Can anyone please explain me the use of this:
int* dynInt = new int;

I know this is a dynamic integer. I know this is useful for arrays, but what is the use with normal variables?
er--- that code is wrong @ computerquip

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int variable[3];
int* firstPointer;

firstPointer = variable;      // OK
firstPointer = &variable[0];  // OK (does same thing)
firstPointer = &(*variable);  // OK (does same thing)

//firstPointer = &variable;   // bad (error)

firstPointer[2] = 3;          // OK (same as variable[2] = 3;
                              //      because firstPointer2 points to variable)

firstPointer = 6;             // bad -- changes firstPointer to point to
                              //      random garbage.  Might throw a compiler
                              //      warning/error depending on the strictness 


EDIT

I know this is a dynamic integer. I know this is useful for arrays, but what is the use with normal variables?


It isn't generally useful for normal variables because you can just make a normal variable instead of making a pointer. What it is useful for is dynamic object creation. IE Polymorphism and inheritance:

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
class Parent
{ /* stuff */ };

class ChildOne : public Parent
{ /* stuff */ };

class ChildTwo : public Parent
{ /* stuff */ };

//---------------------
Parent* MakeObj();

int main()
{
  Parent* p = MakeObj();  // get an object from MakeObj
  p->DoStuff();         // and do something with it

  delete p;
  return 0;
}

Parent* MakeObj()
{
  // get input from user
  int i;
  cin >> i;

  if(i == 0)         // return a different object, depending on what the user selected
    return new ChildOne;
  else
    return new ChildTwo;
}


-- if you didn't get into these concepts yet, don't worry about it too much. Just showing that it will be useful later on, even if it's not useful now.
Last edited on
closed account (S6k9GNh0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    int variable[3];
    int* firstPointer;

    firstPointer = variable;      // OK
    firstPointer = &variable[0];  // OK (does same thing)
    firstPointer = &(*variable);  // OK (does same thing)

    firstPointer[2] = 3;

    *firstPointer = 6;
}
//firstPointer = &variable; I knew this was wrong, not sure what I was thinkin' 
Last edited on
Thank you both Computerquip and Disch

@Disch: ok, thanks. For now, I'll forget it in that case =)
Ok, I'm back with yet another question :(

I've seen now how to pass pointers to a function parameter. But I often see things like:
int* FunctionName(int* par1, int par2, int par3)

I understand what int* par1 does, but I don't really understand how the return type of a function (int* FunctionName) can be a pointer.

Can anyone explain this please? Thanks
it depends why you want a return type of int* or void* or anything..
one of them is this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void some_function()
{
//some code ........................

//here i want to allocate memory of 10 bytes.
int *i_ptr = get_memory_int(10);

//some code ....................
}


int* get_memory_int(int size)
{
int *i_ptr = (int*)malloc(sizeof(int) * size);

return i_ptr;
}





hope this will help you understand.
A bit yes, thanks :)
Topic archived. No new replies allowed.