Array Concept. . . .

Pages: 123
You are passing to it an argument haveing type pointer to an array of 10 elements of type int. So the types of the parameter and of the argument differ.

What do you mean here. I mean i cannot understand. Are you saying i am passing by pointer in one place and on the other place i am passing by reference?
1
2
3
4
5
6
A pointer to character is not an array. Consider

 char c = 'A';
 char *p = &c;

 p is not an array. It refers one object of type char.



No this is not i am saying . You are saying right but i am saying that 'Every array is a Pointer not vice versa'
@ cire: Then what is the ampersand in Line 7?

@ Everyone or at least those who have been here a while: A post about pointers that gets the entire board riled up. Why does this sound familiar?
You are wrong. Arrays are not pointers. You were already shown an example

int a[10];
int *p = a;

std::cout << "sizeof( a ) = " << sizeof( a ) << std::endl;
std::cout << "sizeof( p ) = " << sizeof( p ) << std::endl;

If the array would be a pointer their sizes would be equal.
1
2
Computergeek01 (3078)
 @ cire: Then what is the ampersand in Line 7?


that ampersand is storing address of first element of the array[10]


1
2
3
4
5
6
7
8
9
10
vlad from moscow (5999)
 You are wrong. Arrays are not pointers. You were already shown an example

 int a[10];
 int *p = a;

 std::cout << "sizeof( a ) = " << sizeof( a ) << std::endl;
 std::cout << "sizeof( p ) = " << sizeof( p ) << std::endl;

 If the array would be a pointer their sizes would be equal.



to answer this read this:

sizeof() : When applied to a reference type, the result is the size of the referenced type
and when applied to array gives the size of whole array in bytes becox it has not been referenced DIRECTLY but pointer does have. You can access every element of array just becox it's first element and it's ADDRESS in which you increment to get next elements.


LINK :http://en.cppreference.com/w/cpp/language/sizeof
Edit
Report
@Computergeek01 Everyone or at least those who have been here a while: A post about pointers that gets the entire board riled up. Why does this sound familiar?

I don't have any idea about above posts. If such a post is discussed already then can you give me the link to that post please?
closed account (S6k9GNh0)
Doh, I did forget const. EDIT: I could have sworn there was some funky syntax that allowed you to use pointer syntax to initialize arrays... maybe that was an old GCC extension.
Last edited on
@ OP: I know, the ampersand is called a reference operator, it is not as you described an "address" operator which I'm pretty sure is a term you just made up for this argument. And to address your earlier post, the reason you need to use this is because you can not set a pointer equal to an actual datatype, you need to assign it to the address of that variable or in other words you need to reference it.
This quote

sizeof() : When applied to a reference type, the result is the size of the referenced type


is invalid. When sizeof is applied to a reference type the result is the size of the reference itself not of the referenced type.
Last edited on
@ OP: I know, the ampersand is called a reference operator, it is not as you described an "address" operator which I'm pretty sure is a term you just made up for this argument. And to address your earlier post, the reason you need to use this is because you can not set a pointer equal to an actual datatype, you need to assign it to the address of that variable or in other words you need to reference it.

& as a address operator is common try following:

1
2
3
4
5
int *pointer=NULL;
int number=12;
pointer=&number;
cout<<"address of number is: "<<pointer;
cout<<"read number is: "<<*pointer;



link:

https://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Faddre.htm
Last edited on
And I do not know why you are showing this quote/ What does it prove?! I showed you already that results of sizeof for an array and a pointer are different

Moreover for an array you can calculate the number of its elements the following way

sizeof( array ) / sizeof( *array )

For any pointer this formula gives you an incorrect result.
Last edited on
Arrays and pointers are different type. That is all.
@ cire: Then what is the ampersand in Line 7?

& in that context is (what is often called) the address-of operator. I don't believe the standard refers to it as anything other than the unary & operator. The unary & operator results in the address of the variable it is applied to in the form of a pointer-to-variable-type. Fun fact: the unary & operator can be overloaded.

I notice the OP continues to fail to respond to my post which pointed out that operations legal on pointers are not legal on arrays (not that I expect him to) and continues to make (invalid) arguments about references which don't apply to pointers.

Arrays are not pointers. Pointers are not references.
An array decays into a pointer when passed to a function.

In the function it was defined, you could consider it very much like a const pointer, which the compiler can get size of the chunk of memory it points to. That is why you cannot do this,

1
2
3
4
5
6
int a[10];
++a;

//and you cannot do this for the same reason
int *const a;
++a;


sizeof is able to get you the size of the array in the same function it was declared in because it is calculated at compile time.

Also sizeof() is not a function, but an operator. If it were a function, then it could not possibly return the size of an array, because if you passed the array, it would be passed as a const pointer.


Then why do you have to assign it by reference in Line 7 of your code segment above?

You don't.
http://ideone.com/j6Nxcl
Last edited on
If it were a function, then it could not possibly return the size of an array, because if you passed the array, it would be passed as a const pointer.

if it were a function template, it still could:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

template<typename T, std::size_t N>
std::size_t a_sizeof(T (&)[N])
{
    return N;
}

int main()
{
    int n[123];
    std::cout << a_sizeof(n) << '\n';
}


(which serves to illustrate that if a function expects an array by reference, it gets the array, not some rvalue pointer)
@cubi:
That is interesting. But it is returns 123, instead of 492.
@htirwin

@cubi:
That is interesting. But it is returns 123, instead of 492.



Why do you think that it should return 492? Look the function definition.
Because sizeof gets you how much memory, not how many items. '

I see the point thought that I was wrong in saying that an array is converted to const pointer when passed to a function.

It is converted to const pointer implicitly when you use it in a context that requires it to be one.
Last edited on
Right, to show the size in bytes, make that return N*sizeof(T);
Last edited on
Pages: 123