Article View: csu.cs.200
Article #109Re: Test questions
From: ken shrum
Date: Fri, 09 Oct 1998 00:00
Date: Fri, 09 Oct 1998 00:00
41 lines
1599 bytes
1599 bytes
x1e1r1@concentric.net wrote:
: I must have missed something. What exactly do the extra *'s in front of a
: pointer mean? There were a few test questions about memory addresses that
: had these( int ***Q for example). Anyone?
Something that looks like T* is "pointer to T", for any T. Hence, an
int*** is a pointer to pointer to pointer to int.
A C-ism carried over into C++ is that you often can't tell, when given
a T*, whether this is an array of T or a single T passed by address:
void print_ints(int n, int* arr) {
// int arr[n]
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
void get_length(char* str, int* len) {
// return len of str in len
*len = 0;
if (str != 0)
while (*str) {
str++;
(*len)++;
}
}
In print_ints, the int* is an array. In get_length, the int* is an
"output" parameter.
It's common to see, for example, an int**. This is often an array of
ints passed by address:
void get_array(int len, int** arr) {
*arr = new int[len];
}
More than two '*'s is pretty infrequent.
Ken Shrum
Technical Details
Message-ID:
<6vl3s9$2qs@alain.cs.colostate.edu>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<6vk3sl$mie@journal.concentric.net>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 127144,6753cd57f9c4346 X-Google-Attributes: gid127144,public From: shrum@alain.cs.colostate.edu (ken shrum) Subject: Re: Test questions Date: 1998/10/09 Message-ID: <6vl3s9$2qs@alain.cs.colostate.edu> X-Deja-AN: 399351146 References: <6vk3sl$mie@journal.concentric.net> Organization: Computer Science Department, Colorado State University Newsgroups: csu.cs.200