This question already has answers here:
C++ * vs [] as a function parameter
(3 answers)
Closed 9 years ago.
Consider this piece of code:
char strName[25];
void SetInfo(char *strName)
{
strncpy(m_strName, strName, 25);
}
Why are they using a pointer in the function parameter? Can't we just do this:
void SetInfo(char strName[]) {
strncpy(m_strName, strName, 25); }
? What is the difference between both?
Thank you
In this particular case, none at all (aside from one more letter to type). A char array "decays" to a pointer when passed to a function.
arrays always decay as pointers when passing as parameters.
in this case array is like a pointer (they point to the memory of the first element of the array)
void foo(char a[])
{
a[0] = '#';
cout << a[0];
cout << *a;
}
Both calls to cout prints the same character '#'.
void foo(char a[])
{
// gives you the size of a pointer to the array's data type.
// prints 4
sizeof(a);
// prints char *
cout << typeid(a).name();
}
I'm not sure if this is standard.
Related
This question already has answers here:
How to print function pointers with cout?
(7 answers)
Closed 6 years ago.
I understand functions like data items have addresses and the address of a function is the memory address where the stored machine language code for the function begins. I have this code;
#include <iostream>
using namespace std;
int x(int);
char * y(char *);
int main() {
x(5);
y("hello");
int (*p) (int); //pointer to function x
char * (*q) (char *); //pointer to function y
p = &x; //holds the address of function x
q = &y; //holds the address of function y
cout << p << endl;
cout << q;
return 0;
}
int x(int a) {
return (a * a);
}
char * y(char *b) {
return (b);
}
So is there a way i can force the cpu to execute a particular function before another using the function addresses?
Upon compiling, the program prints out both addresses as 1. I was expecting hexadecimal values like that of data items. even when I print the dereferenced values, I still get 1, what is going on?
Also, if both function addresses are 1, how does the cpu know which function to execute first?
EDIT:
one of my questions is left unanswered, which i find very important! Doesn't wholly make it a duplicate even though some are.
The std::cout will convert function pointers to a bool, and since the function pointer is non-zero, the value 1 is displayed.
To display as hex value, cast the function pointer to a void *, for example:
cout << (void*)p << endl;
You can retrieve these functions addresses by typing :
cout<< &p << endl << &q << endl;
The & operator gives you back the address of block memory in which the variable after the & operator is set.
This question already has answers here:
Printing array element memory adresses C and C++, why different output? [duplicate]
(4 answers)
Closed 6 years ago.
Refer the code below:
#include <iostream>
class Boy {
char name[10];
public:
void show() {
*name = 0;
std::cout << "\n" << &name[0];
}
};
int main() {
Boy b;
b.show();
}
Here, why don't we see the address of name[0]. I also tried with name, which itself is address. Still I can't see the address, it returns blank screen.
It's because you're using char* overload for operator<<, which treats the pointer as a pointer to c-string. Cast your pointer to void* to print it as such.
std::cout << "\n" << static_cast<void*>(&name[0]);
This question already has answers here:
Passing an array as an argument to a function in C
(11 answers)
Closed 7 years ago.
I read here:
C: differences between char pointer and array
that char pointers and char arrays are not the same. Therefore, I would expect these to be overloading functions:
#include <iostream>
using namespace std;
int function1(char* c)
{
cout << "received a pointer" << endl;
return 1;
}
int function1(char c[])
{
cout << "received an array" << endl;
return 1;
}
int main()
{
char a = 'a';
char* pa = &a;
char arr[1] = { 'b' };
function1(arr);
}
Yet upon building I get the error C2084: function 'int function1(char *)' already has a body. Why does the compiler seem to consider a char pointer to be the same as a char array?
Because when you pass an array into a function, it magically becomes a pointer.
Your two functions, then, are the same.
The following are literally* identical:
void foo(int arr[42]);
void foo(int arr[]);
void foo(int* arr);
(* not lexically, of course :P)
This historical C oddity is the major reason lots of people mistakenly think that "arrays are pointers". They're not: this is just a bit of an edge case that causes confusion.
This question already has answers here:
How do I use arrays in C++?
(5 answers)
What is array to pointer decay?
(11 answers)
Closed 8 years ago.
As far as I know, in C++ when you pass a non-pointer object to a method, it makes a copy of it to work with in the method. However in my program below, I pass a copy and yet my method actually edits the original character array. Why is this working? :/
#include <iostream>
#include <string>
void Reverse(char s[], int sizeOfArray)
{
for (int i = 0 ; i < sizeOfArray; i++)
{
s[i] = 'f';
}
}
int main()
{
char c[3] = {'g','t','r'};
Reverse(c,3);
for (int t = 0 ; t < 3; t++)
{
std::cout << c[t];
}
return 0;
}
NOTE:
The output is fff
You cannot copy arrays by passing them to functions. The array "decays" into a pointer. Check for yourself by printing the variables' typeid:
#include <iostream>
#include <string>
void Reverse(char s[], int sizeOfArray)
{
std::cout << typeid(s).name() << "\n";
for (int i = 0 ; i < sizeOfArray; i++)
{
s[i] = 'f';
}
}
int main()
{
char c[3] = {'g','t','r'};
std::cout << typeid(c).name() << "\n";
Reverse(c,3);
for (int t = 0 ; t < 3; t++)
{
std::cout << c[t];
}
return 0;
}
Result:
char [3]
char *
Moral of the story:
Use std::vector.
Edit: I should mention that the exact result of the typeid name is implementation-defined. "char [3]" and "char *" is what I get with VC 2013. But the underlying issue is the same on every compiler, of course.
void Reverse(char s[], int sizeOfArray)
Reverse(c,3);
>call by value
>call by reference
here you are doing call by reference operation that means you are passing address of c to int s[]
you are passing a reference of c to reverse function. this is called call by reference not call by value. thats why reverse function overriding your original input
Because char s[] is actually char * pointing to the first element of array.
It means your function Reverse gets the first arg pointer but not copy of array.
If you want to get copy you should use memcpy first and pass new (copy) array to function.
C-array cannot be passed by copy.
It may be passed by reference as void Reverse(char (&a)[3])
or by its decayed pointer as you do void Reverse(char a[], int size)
(which is the same as void Reverse(char* a, int size)).
You may use std::array (C++11) to have a more intuitive behaviour.
if you declare an array then the variable holds the base address of that array, so here char c[3] means c holds the base address of the array c[3].
so, when you are passing Reverse(c,3); actually you are passing the base address.
This question already has answers here:
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
Passing Arrays to Function in C++
(5 answers)
What is array to pointer decay?
(11 answers)
Closed 9 years ago.
I made a function in C++ to find the length of an array. I find the sizeof the array passed in the argument and divide it by the sizeof the variable type. This should work but it always returns 1! Am I missing something obvious? Or does this have to do with pointers and memory? This is my code:
#include <iostream>
using namespace std;
int lengthOf(int arr[]);
int main() {
int x[] = {1,2,3,0,9,8};
int lenX = lengthOf(x);
cout << lenX;
return 0;
}
int lengthOf(int arr[]) {
int totalSize = sizeof arr;
cout << totalSize << endl;
int elementSize = sizeof(int);
return totalSize/elementSize;
}
Output (should be 6 instead of 1):
4
1
I am fairly new so excuse me if this is a bad question.
When passing an array as parameter, it always decays into a pointer. If you want to see the size of the array, you need to pass the array by reference, e.g.:
template <int Size>
int lengthOf(int (&array)[Size]) {
return Size;
}
You should use the pointer.
(sizeof(arr)/sizeof(*arr))
Even though int arr[] looks like you are passing an array, you are actually passing a pointer. int arr[] is equivalent to int* arr when used as a function parameter, this comes from C.
In C++, if you want to pass an array, the proper way is to do it by reference:
template <int N>
int lengthOf(int (&arr)[N]) {
int totalSize = sizeof arr;
cout << totalSize << endl;
int elementSize = sizeof(int);
return totalSize/elementSize;
}