1. ## Pointers

PLEASE CAN SOMONE EXPLAIN POINTS TO ME IN SIMPLE TERSM!! WHAT ARE THEY? CAN YOU RELATE IT TO AN EVERYDAY SCENIRIO? I CANNOT GET MY HEAD AROUND THEM

2. A pointer is just a variable that holds a memory address.

I'm not a very good teacher, so my analogy might just be that terrible...

int *y;
y is now a pointer to an integer. The value of y is the memory address that it references (which right now is NULL). The value of *y is the value of the memory address that it references.

example:

int k = 7;
int *y;

y = &k;
y equals the address of k. y now points to the memory address of k. *y is equal to 7.

Just think of a variable declared as a pointer as a box. The box should always have a line from it to memory (to what it's referencing). If you give the box a new value think of yourself drawing a new line from the box to somewhere else in memory.

I hope this terrible explanation helped you a little bit.

Do you also need help with pointer arithmetic?

3. THERE ARE MANY EXPLANATIONS ON THIS SITE ALREADY. USE THE SEARCH BUTTON. BY THE WAY, YOUR CAPS LOCK BUTTON IS STUCK!

4. A pointer is just an address. If I gave someone a letter for Marcus, they may not know who Marcus is and would require the address for Marcus. Variable names are generally lost when you exit a function, so you use addresses, which don't change. You've probably noticed that often something will have a variable name in main which is different from its name in a function. The address is what links them together.

5. I know a pointer is a variable that holds an address... what i don't understand is why this works:

char *a = "String";

Is it the same as

char arr[] = "string";
char a* = arr;

?

6. Now a and arr are pointing to the same string. (char *a instead of char a*)

7. right, i get that.

I understand:

char arr[] = "string";
char *a = arr;

you assign "string" to an array arr, and the system counts the string for you effectively putting "string" into arr[6].

What i'm not so sure about is what is going on here (this being a seperate example from the above):

char *a = "string";

how can a pointer point at a string that hasn't been put into an array?

8. > char arr[] = "string";
> char *a = arr;
>
> you assign "string" to an array arr, and the system counts the
> string for you effectively putting "string" into arr[6].

Slight correction here. The assignment of "string" to 'arr[]' puts
those letters in the first six characters, of 'arra', and adds the
NULL in 'arr[6]'. I believe this is what you meant to say.

> char *a = "string";
>
> how can a pointer point at a string that hasn't been put into an
> array?

This is called a "string literal". A strin literal is a block of memory
set aside to house a string declared in this manner. You (if I
recall correctly) cannot free( ) string literals. They are allocated in
the string table, and are permanantly afixed there for the life of
the application.

Furthermore, if you do this:

char *a = "string";
char b[] = "string";

a = b;

Then the assignment causes you to loose your string literal. It
still exists some place, but you no longer have access to it due
to the fact that nothing points to it any more.

Quzah.

9. Originally posted by Clyde
how can a pointer point at a string that hasn't been put into an array?
Either it's on a heap or a stack.......one of the two

10. Thanks for the explanation quzah.

Is there any reason why you would want to place a string in a string literal rather than an array?

11. >why you would want to place a string in a string literal rather than an array?
If you don't want to modify the string, declare it as a string literal. That way if you accidentally try to modify it, you'll get an error.

-Prelude