Pointers, Arrays, General Questions

Hi everyone,

First, this is my first post here, even though I've been using this tutorial for a couple of years. I regained my interest in C++ because of a class I'm currently in. I have some general questions about C++.

1) Apparently, you can pass variables by reference instead of by value to a function (using the & symbol). This allows you to make changes to the variable within the function. As I began to read up on pointers, I realized that these two concepts seem closely related. When you have a function such as void function (int& a, int& b), are you really just passing the addresses of a and b to the function? (After all, in the tutorial page on pointers it says that & just means "address of".) It seems that you can also do something like void function(int* a, int* b), which seems to be directly using pointers. Can someone please explain if these two techniques are the same or what the difference between them is?

2) I read that pointers and arrays are very similar. Is there a difference between the two when storing multiple consecutive variables? Can you declare a pointer, then start saving to memory blocks after what the pointer is pointing to, simulating an array? It seems like you can, but couldn't one of these blocks coincidently be the location of a different variable being used in the program?

3) Also, can you declare an array of unknown length? For example, reading integers from a text file is easy using the class "ifstream". And you can read until the end of the file using "eof()". Can you keep saving to array members until the end of the file is reached (with an unknown amount of entries) and if so, how would you do this?

4) Can anyone give me any ideas for simple projects to practice programming in C++? I want to practice what I've learned, but I can't think of anything that will be challenging, but not impossible, and can keep my attention.

Thanks for any help.

Alex
Hi.

1) You've got it. No matter how it is termed or what goes on behind the scenes, ultimately "int &a" is just a fancy-pants way of saying "int *a" (but with all kinds of useful protections built-in).

2) Pointers are not arrays. Check out the "Articles" page of this website. There are a good number of postings there all about arrays and pointers.

The thing to remember is that the name of the array, all by itself, is roughly equivalent to a pointer, in that it yields the address of the first element of the array. So the following are exactly equivalent:
1
2
3
  int a[ 42 ];
  a[ 7 ] = 0;    // same as...
  *(a + 7) = 0;  // ...this 

The variable "a", however, is const. You cannot change the address of the beginning of the array by assigning to it.

3) No. Arrays must have length.

That said, you _can_ typecast to an array of unknown length. Remember, the array already exists --it has length-- only the compiler doesn't know what it is. You will occasionally see things like
1
2
3
4
5
6
struct tag_string {
  unsigned length;
  unsigned capacity;
  unsigned reference_count;
  char data[];
  };

Likewise you will see functions with parameter lists like
void my_foo( int a[], unsigned length );
If this is beyond you right now, don't worry about it. You'll learn in time. (Alas I'm too tired to explain more than this ATM. Sorry.)

4) What kind of program would you like to see?

Hope this helps.
Last edited on
Thank you, Duoas.

I understand now. If "a[]" is an array, then "a" contains the address of the first element and "a[0]" is the same as "*a" which contains the variable.

I haven't learned about typecasting, so I'll just accept your answer for now.

As far as example programs, it seems like the only programs I've been writing are to demonstrate how a certain feature works, but not why you would use it. I just want some ideas for simple projects that I can use to test my understanding of the concepts as well as to practice designing a program.

I'm open for any ideas.
Topic archived. No new replies allowed.