Hi,
I have a method call say some fucntion that takes in a pointer of an array of char

char  anArray[32]= 
           {'1', '2', '3', '4', '5', '6'};

// the function call 

someFunction(&anArray[0])

I don't think I have quite grasped the concepts of pointers yet, does the code i have written above apply a pointer to a function ?

thanks

Recommended Answers

All 7 Replies

you could use a technique called 'pointer arithmetic' to access ye' array:

#include<cstring>

//dynamic array
char* array = new char[11];

//breaking into the ol' C library to handle the array cstring
strcpy(array, "I Love C++");

//display the array using pointer arithmetic method:
while(array != NULL)
{
     cout << *array;
     array++;
}

No subscript operator needed; you are using pointers and pointer arithmetic to move about in system memory.

Here is a pointer tutorial from DaWei

you could use a technique called 'pointer arithmetic' to access ye' array:

#include<cstring>

//dynamic array
char* array = new char[11];

//breaking into the ol' C library to handle the array cstring
strcpy(array, "I Love C++");

//display the array using pointer arithmetic method:
while(array != NULL)
{
     cout << *array;
     array++;
}

No subscript operator needed; you are using pointers and pointer arithmetic to move about in system memory.

thanks but when i try and use strcpy it does not like the amount i have in the array. and
@ancient dragon - just gonna read that now thanks

solution for other peoples future referances

char  anArray[32]= 
           {'1', '2', '3', '4', '5', '6'};
      
 
 char*  pAnArrray = anArray;
// the function call 
 
someFunction(&anArray[0])

Here is the real solution

char  anArray[32]= "123456"; // <<< This is how to initialize the array with some text
      
 
// char*  pAnArrray = anArray; <<< This is an unnecessary pointer
// the function call 
 
someFunction(anArray) // << this is how the array should be passed to a function

Here is the real solution

char  anArray[32]= "123456"; // <<< This is how to initialize the array with some text
      
 
// char*  pAnArrray = anArray; <<< This is an unnecessary pointer
// the function call 
 
someFunction(anArray) // << this is how the array should be passed to a function

thanks - i was told by the person who set it should for this particular program it should point to an array.
Thank you though i will note it down for future reference

It does point to an array. What I posted is the general way to pass arrays as parameters. What you posted will work too, but the & address symbol is not necessary because all arrays are passed by reference (or by address), never by value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.