You are here:

C++/Strings, Arrays, and Pointers


Hi, can you help me with some questions about strings, arrays, and pointers?

1) what is the relationship between the 3?
2) can you explain why, in the following piece of code, f[10] and g[10] do not need to be initialized:
int *i, j[10];
double *f, g[10];
int x;

i = j;
f = g;
for (x = 0; x < 10; x++) cout << i+x << ' ' << f+x << '\n';

and why are the outputs very weird number? Are those addresses? Why?

3) In the following code, why is & operator not used in start = str as well? what does end = & str[len-1] and while(start < end) do?
char str[] = "this is a test";
char *start, *end;
int len;
char t;

cout << "Original message: " << str << "\n";

len = strlen(str);
start = str;
end =& str[len-1];

while(start<end) {
t =* start;
*start = *end;
*end = t;
end--; }

cout << "Inverted message: " << str << "\n";

4) can you explain the following code? what does *p2 && *p2 == *p means and !*p2 do?

char *get_substr(char *sub, char *str) {
int t;
char *p, *p2, *start;

for (t = 0; str[t]; t++) {
 p =& str[t];
 start = p;
 p2 = sub;
 while (*p2 && *p2 == *p) {
    p2++; }
 if(!*p2) return start;
 return 0;}


Hello Angela, thank you for the question.

1) A string is a name for an array of characters. Every array is a pointer (the name itself of the array is an implicit pointer to the first object in the array). If you want to create a string, it can be done with a character pointer or character array:

char array[5] = "test";
char *pointer = "test";

if you were to cout either of those, they would both print test.

2) Since every array is a pointer, by creating a double *f, and then a double g[10], you can assign f to g, and then f is a pointer to the first element in 9. They would both be assigned to the same memory address, as that's all a pointer is. A variable that stores a memory address as its value. When you do the for loop, you cout f + x. This is going to print out whatever the value of the memory address f is, + the value of x. If you wanted to see the values in the array, you index, and cout << f[x]. Please note that because the values in the array g are never provided, these will be random integer values.

3) The & operator returns the address of a variable in memory. This is often used to assign pointers to values, since a pointer stores a memory address.

str is a character array, and start is a pointer. So assigning start to str doesn't require the &, as the name of an array is a pointer to the first item in it.

When you index into an array, that gives you the value at that address. str[end-1] is a value in the array, so using the & operators stores the address of that value to the pointer end.

4) When you use the * operator on a pointer, it returns the value of stored at the memory address the pointer points to. For instance:

int p = 5;
int *x = &p; // Assign the pointer to the address of p
cout << *x; // Will print 5, since at the address of x, is the value 5

Since it is doing *p2, that is saying while the value stored in p2 isn't NULL. while *p2 == *p is looping while the values they store are equal to each other.

The if(!*p2) says if the value stored in p2 is NULL. Using ! is shorthand for saying if(*p2 == NULL).

I hope this was helpful.

- Eddie


All Answers

Answers by Expert:

Ask Experts




I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by I also know a good deal about graphics via OpenGL, and GUIs.


I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 All rights reserved.