Two C/C++ questions :)

Started by
3 comments, last by Zahlman 17 years, 6 months ago
I just had some C/C++ curiosities I was hoping people could help me answer. For instane consider:

void func(){
     char szBuf[100];

     // error: how come I can't do this?
     szBuf++;
  
     ...

}

As we know the name of an array is a pointer, how come in that case I cannot increment the pointer? I get errors regarding not having an l-value. Secondly if I have a function like:

void func(int* i);

Is there a way I can pass a parameter in this case without it having been declared previously? Something like: func(&(5)) ... though that won't compile.
Advertisement
Quote:Original post by fpsgamer
As we know the name of an array is a pointer, how come in that case I cannot increment the pointer? I get errors regarding not having an l-value.


The name of the array is equivalent to a _constant_ pointer to the first element of the array. The pointer's value cannot be changed.

Quote:
Is there a way I can pass a parameter in this case without it having been declared previously? Something like: func(&(5)) ... though that won't compile.


No. You need to pass in a pointer - an address of an integer in memory. Your constant effectively has no address, it is buried in your program's code rather than being a seperate item of data.
Let me clarify this...

Arrays are NOT pointers, and vice versa. There is a difference. A pointer does have the same syntax functionality as an array for ease of use. Though the accomplish the same thing, there are differences. Arrays are on the stack, allocated memory on the free store; arrays are blocks of data, pointers are memory addresses. There's a few more...

Anyways...

On your question about the pointer, no...

The constant there needs to be initialized as an integer variable.

You could create a reference function however, and put the constant in. Works for std::strings at least, so I think it will initialize a new object and pass that.
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Quote:Original post by dbzprogrammer
You could create a reference function however, and put the constant in. Works for std::strings at least, so I think it will initialize a new object and pass that.


Do you mean this:
int foo(int & param);foo(5);


If so, that will not work - this is disallowed because foo almost certainly modifies param, yet you have no way of seeing any changes to the "constant" you have passed in. Using a constant reference,
int foo(const int & param);
is allowed, however.
Slightly different ways of looking at it:

1) The name of the array is *usable as* a pointer. But the array still is an array, which means it's a specific chunk of allocated space in a specific location. It can't just magically move to a different place, and '++' on a pointer modifies the pointer value, which would require the array to move.

2) The function is allowed to change the pointed-at value. Even if you could do this, somehow - how would you get to see the changes? But no, "the pointed-at value" in this case doesn't necessarily have any identity anyway. (When you call a function by value, and pass a constant, the constant can be loaded directly into a register. To call it by pointer, the constant would have to be loaded somewhere else with an actual memory address, so that a pointer to that somewhere-else could be loaded into the register, and the language doesn't provide for that. However, in C++, you can get this effect with a const reference.)

This topic is closed to new replies.

Advertisement