Computing.Net > Forums > Programming > C++ Array Pointer Parameter

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

C++ Array Pointer Parameter

Reply to Message Icon

Original Message
Name: muska
Date: May 13, 2003 at 08:31:13 Pacific
Subject: C++ Array Pointer Parameter
OS: Windows/Console
CPU/Ram: N/A
Comment:

In wrinting a program for computer science, I have a two-dimensional char variable,

char salesMan[10][25]

in which I want to store an array of names of salesmen.

I want to pass this array to a function as a pointer (so I can edit it withing that function). The function's prototype is as follows:

void inputSalesmanData(char *salesMan, float *salesFigure);

When I can the function, I have to use the following for it to work:

inputSalesmanData(*salesMan, salesFigure);

apparently with another pointer (asterisk in front of the variable).

Why is this, and is it correct? Is there a better way to do this?


Report Offensive Message For Removal


Response Number 1
Name: Dvlstx
Date: May 13, 2003 at 09:57:33 Pacific
Reply: (edit)

Basically what is happening is you are passing a pointer to the name of the salesman to the function. The reason I know this is because of your prototype statement. It accepts a pointer. So in order for it to work you MUST send the function a pointer to the name.

Does the function work? if yes, then it is correct, if not then it is not correct. It appears correct to me and it makes sense to me for why you have to do it that way.


Report Offensive Follow Up For Removal

Response Number 2
Name: muska
Date: May 13, 2003 at 11:33:22 Pacific
Reply: (edit)

In a previous program, I had a function that accepted a pointer to a one-dimensional array. When I called that function I did not have to put an asterisk in front of any arguments as I did when I called this function. For example:

void inputFileData(char *arrayRef, char delimeter);

char strEvent[50];

inputFileData(strEvent, '\n');

But if it works, it works.


Report Offensive Follow Up For Removal

Response Number 3
Name: Sam
Date: May 15, 2003 at 18:24:38 Pacific
Reply: (edit)

Well. The first thing you must have understood is the way that C implements the arrays. Basically in C a pointer and an array is the same thing. A pointer is a 4bytes (or 2 sometimes) integer variable. The value of the pointer represents a memory address. You declare a pointer to char this way:

char* pname;

You take a char's address using the & operator.
eg char c;
pname = &c; // pname holds c's address.

You have access at the memory position a pointer "points" using the * operator.
eg *pname = 'f' // the char that is at the position pname points, changes to 'f'

An array is tha same thing. It is a pointer to a memory address. The difference here is that you have more than one pointed items.

eg. char ar[10];

ar is of type char*. The system allocates memory space to hold 10 chars. The memory positions of that chars are sequential. if the first is at the memory position 1000 the second is at 1001 and so on until the tenth which is at 1009.

The variable ar is a pointer that holds the memory address of the first of those 10 chars.

The expression (ar) is the same with (&ar[0]). ar is a pointer to the first char, thus it's value is the address of the array's first element.

(ar[0]) is the same with (*ar). ar[0] is the first char and *ar is the char that ar points to (and ar points to the first char). (ar[1]) is the same with (*(ar + 1)). ar[1] is the second char of the array, and *(ar + 1) is the char that ar points to if you add it with 1. (1000 + 1 = 1001 which is the address of the second char if the first's char address is 1000).

So. Arrays ARE Pointers. But pointers ARE variables too and they ARE stored in memory so they HAVE addresses too. The address of ar can be taken like this : &ar. This value is a pointer that holds the address of ar. ar is a pointer that holds the address of a char. So ar's address is of type char** (pointer to a pointer to char). This is recursive. You can have pointers to pointers to pointers to something and so on.

So. As you can have arrays of (pointers to) ints, you can also have arrays of (pointers to) pointers too. Eg.

char* String[10];

String is an array of 10 pointers to char. This means String[0],String[1],... are pointers to chars. BUT ar was an array of chars which means that ar was a pointer to char. Yes. And Each element of String is a pointer to char. Yes. That means that each element of String is an array too. YES. So String is an array of arrays. YES.
String is an array of pointers. But arrays and pointers is the same concept. So String is an array of arrays. String[0] is an array of chars. String[1] is an array of chars too. This is the two-dimensional array. And this is the way C implements the multi-dimensional arrays. A 2d array is an 1d array of 1d arrays.
A 3d array is an 1d array of 2d arrays.
and so on.

String is a pointer to char* (because each element is a pointer to char) so String is a char**.

In your example
char salesMan[10][25]

salesMan is a char**. (array of arrays of chars)
salesMan[0] is a char*. (array of chars)
salesMan[0][0] is a char;

don't forget. in C Arrays ARE pointers. The array name is a pointer that points to the first element of the array.

salesMan points to salesMan[0], which is char*.
SalesMan[6] points to salesMan[6][0] which is char. // The first char of the 7th array of salesMan

So if you want to pass salesMan as an array to a function you must pass a char**.

eg

void myfunc(char** salesArray);

In the function body you can use salesArray as a normal 2d array using [][].

eg char c = salesArray[6][4];
or
char d[20]; // an array of chars

strcpy(d,salesArray[5]); // this copies the 6th string of salesArray to array d.

if the dimensions are not known to the function you can pass them as arguments too.
eg
void myfunc(char** array,int rows,int cols);

eg

char names[60][20] // This holds 60 names of at most 20 characters each.

myfunc(names,60,20); // myfunc takes a char** as argument and names IS a char** because it is an array of char*, or else an array of arrays of chars, or else a pointer to pointers to chars, thus a char**. :)
----------------
Your code is syntactically correct.

inputSalesmanData takes as an argument a char*
You call this function in this line

inputSalesmanData(*salesMan, salesFigure);

salesMan is a char** so *salesMan is a char*. (That is what the function takes as an argument)
This is exactly the same with this call

inputSalesmanData(salesMan[0], salesFigure);

Because salesMan points to the first char* in the array which is salesMan[0].

You pass an array in the function but not the whole 2d array. Only its first row, salesMan[0].

If you want to pass the whole 2d array you must declare the function like that

void inputSalesmanData(char **salesMan, float *salesFigure);

and call it like this

inputSalesmanData(salesMan,salesFigure); // again salesMan IS char** (2d array)

(I believe salesFigure is a float* and you have a reason to pass this pointer here. If you just want to pass a float and don't want to change it inside the function's body, there is no need of a pointer here. just pass a float, not a float*)

Believe me i was confused while i was writing this, but i hope some things about pointers and arrays are more clear now. :( (Are they??) :)

S.A.Y.S. Sam. (Sam At Your Service)

(Sorry for my english. I'm Greek and never tried to explain something in a language other than mine. :) )


Report Offensive Follow Up For Removal

Response Number 4
Name: muska
Date: May 16, 2003 at 08:22:33 Pacific
Reply: (edit)

Well you did VERY well and clarified a lot of things very well. You sure know your stuff.

So basically a pointer is the same as a reference to a variable, but for the entire array...

But why do you say

int main(int argc, char** argv)

(not sure if that's it exactly) with a pointer pointing to another pointer in the argv variable to except parameters to a program?


Report Offensive Follow Up For Removal

Response Number 5
Name: muska
Date: May 16, 2003 at 08:30:18 Pacific
Reply: (edit)

Wait...I think I get it. You can write it either

int main(int argc, char **argv)

or

int main(int argc, char *argv[])

Since, as you said, a pointer is just pointing to the location of the parts of the array in memory, either way works.

Then you have a pointer in front of argv, *, so that the contents of that array can be modified in the main function.

So if I were to just say

int main(int argc, char argv[])

it should work, but I would just be unable to modify the values of the array.

Is this correct?


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you own an iPhone?

Yes
No, but soon
No


View Results

Poll Finishes In 7 Days.
Discuss in The Lounge
Poll History




Data Recovery Software