pointers, array, functions

Pages: 12
Jun 6, 2014 at 6:13am
danicpp wrote:
myarray is same as &myarray[0]


<Technicality>
myarray can be implicitly cast to the same thing as &myarray[0]. However they are not the same thing -- there is a subtle difference.

myarray is an array.
&myarray[0] is a pointer to the first element.

However.. an array can be cast to a pointer to its first element. But that only happens when necessary.

This trips up newbies most commonly when they try to use sizeof:

1
2
3
4
5
6
7
8
9
cout << sizeof(int);   // assume this prints 4
cout << sizeof(int*);  // assume this prints 4

int array[10];
cout << sizeof(array);     // prints 40 -- the size of the array
cout << sizeof(&array[0]); // prints 4 -- the size of a pointer

int* ptr = array;     // legal assignment -- being implicitly cast
cout << sizeof(ptr);  // prints 4 -- the size of a pointer 


</Technicality>
Last edited on Jun 6, 2014 at 6:15am
Jun 6, 2014 at 8:13am
thanks cire I get it now a pointer will get a NULL value when we put a NULL value in it but still we cannot dereference it unless we don't put a valid address in it

well void I thought means nothing void written or not written have no effect(in functions at-least) but still I guess C++ recognizes nothing as void I guess, so at declaration time if we want variable to be of no type then we ought to tell c++ with void keyword

Did I get it all right???
Jun 6, 2014 at 8:20am
yes Disch makes sense, there is difference between array and &array[0] but array can cast this( &array[0] ) when necessary

thanks for the help, I guess I'm improving my knowledge much better way.
Jun 6, 2014 at 4:24pm
well void I thought means nothing void written or not written have no effect(in functions at-least) but still I guess C++ recognizes nothing as void I guess, so at declaration time if we want variable to be of no type then we ought to tell c++ with void keyword


You can't have a void variable -- that wouldn't make any sense.

There are 3 general uses for void that I can think of:

1) As a return type to a function:
1
2
3
void function()
{
}


The 'void' cannot be omitted here. This indicates that the function does not return any value.

2) To indicate that you want a function to take no parameters:
1
2
3
4
int function(void)
{
    return 0;
}


This is a C thing and not really C++.
In C++, no parameters means the function takes no parameters.
In C, no parameters means the function can take any number of parameters (similar to the C++ ellipsis)

So in C, to explicitly say the function has no parameters, you put the void in there. However this is completely unnecessary in C++.



3) void pointer:

 
void* ptr;


A void pointer is another C-ish concept. The idea is that it's a generic pointer that can point to anything -- the data pointed to can be of any type.

This is typically avoided in C++ because it's not type safe.
Jun 6, 2014 at 4:42pm

The 'void' cannot be omitted here. This indicates that the function does not return any value.


but in main(){} I guess we don't need to mention void cause actually I don't and it compiles successfully.

Well but I checked right now that I cannot declare custom functions without void keyword it says ISO C++ forbids.... so
myfun(){}
void myfun(){}

void pointer!!! but when we *(ptr + 1) how does ptr know to jump 1byte or 4bytes or more bytes????
Jun 6, 2014 at 5:07pm
danicpp wrote:
but in main(){} I guess we don't need to mention void cause actually I don't and it compiles successfully.
The return type of main is int (and you must declare it as such).
So this is legal:
int main()

while these are not:
main()

void main()

long double main() // my username, lol
Some compilers will compile one or both of the first two of those above, but that doesn't mean it's legal C++.

void pointer!!! but when we *(ptr + 1) how does ptr know to jump 1byte or 4bytes or more bytes????
You can't use + with a void pointer exactly because of this.
Last edited on Jun 6, 2014 at 5:09pm
Jun 6, 2014 at 5:14pm
but in main(){} I guess we don't need to mention void cause actually I don't and it compiles successfully.
main must return type int. If your compiler compiles main(){} without complaint, it is not conformant. If you're compiling as (pre-C99) C and not C++, the return type of a function without a return type specified is assumed to be int.


void pointer!!! but when we *(ptr + 1) how does ptr know to jump 1byte or 4bytes or more bytes????

You cannot do pointer arithmetic with void pointers.
Last edited on Jun 6, 2014 at 5:14pm
Jun 6, 2014 at 6:03pm
thanks to all of you my mid exams are starting day after tomorrow so my cplusplus use may reduce but I will start again soon around June 17th

Thankyou all
Topic archived. No new replies allowed.
Pages: 12