First, recall that pointers are variables which contain an address to some other variable. So if you have a variable
int* n, this defines a variable
n, which contains the address of some place in memory of where to find an integer.
Each location in memory is given a unique number. Pointers are numbers which refer to locations in memory. So a pointer with the value
1000 will refer to whatever happens to be at the
1000th location in memory.
What is the difference between char word[length] and char *word = malloc(sizeof(char) * LENGTH)??
A program written in C uses two places to store things, namely the stack and the heap.
The stack is used to store local variables. Any variable you type out in code will be stored on the stack. When a function is called, all the variables in the function are added to the stack while the function is running. When the function returns, all the variables are removed from the stack. This is referred to as static memory since the size cannot change while the program is running.
The heap is used to store things which can change while your program is running. This is called dynamic memory since it can change. While the program is running it will calculate how much memory it needs, and "allocate" that space on the heap. The operating system (OS) will find an unused chunk of memory of the requested size, which no-one else is using, and return a pointer to it, so that the program knows where to access the memory. This is what
char *word = malloc(sizeof(char) * LENGTH) is doing - it's asking the operating system to find it a piece of memory which is
LENGTH number of
One analogy might be to think of the stack like your house, and the heap as everything outside it. You lay out the plans for your house by writing code. When the program runs, it's like the house is built from the plans. Once the house is built it is a certain size and shape and (generally) does not change.
Now say you want to keep some elephants (a large amount of data). You could keep the elephants in some rooms in your house (keep data on the stack), but then you could run out of space if you design your house too small, or you could end up wasting space if you design your house too large.
You could also just keep them outside (allocate data on the heap). So you ask the city (the operating system) for a zone to keep your elephants. The city allocates some land, and gives you a note with the address of where your elephant is kept. This is the pointer to the elephant. When you want to look at the elephant, you read the address off the note, and go to that location.
You still keep the pointer (the letter) in the stack (the house), but it takes up much less space in your program compared to storing the data (the elephant). The other advantage is if you need space for more data (more elephants), you only need to ask the city (the operating system) for more space.
In char * word, what is the meaning of &word, word and *word?
word is a the location of a
char variable somewhere in memory. There may be many chars stored after this location, but the pointer refers to the first
char. This will be some number which is a memory location, e.g.
*word is the value that
word points to. In the case of a
char, this will refer to some ASCII character, e.g.
&word in this context makes little sense. It's basically getting the location of
word, or in other words, converting into a pointer. In your example
word is already a pointer, so you're asking for the address of a pointer.
Some vocabulary might help when reading and writing code dealing with pointers.
First rule, read pointer definitions from right to left. For example:
char * word
Read this as
word is a
* (pointer to) a
When you write
n = *word, or
*word = 'A', you are dereferencing the pointer, or in other words, "going to the location that the pointer refers to". Another way to think of this, is you are converting a pointer back into the type that it refers to. So in your scenario
*word, means "get the value that
word points to".
& is the "address of" operator. This is saying, get the location in memory of where a variable is stored. For example, if you have a variable
char word, using
&word will give you a pointer to
What is the meaning of NULL in pointer, does it mean word points to memory but memory have nothing OR word doesn't point to memory at all in case word has NULL?
NULL is directly equivalent to
0. Using your earlier example, if you have a variable
char* word = NULL, this is like saying
char* word = 0. In theory it's referring to the very first byte (the zeroth byte) in memory, although in practice the program takes this to mean that it refers to nothing.
In other words,
word will have an address which is exactly
0. Using the prior example of the letter, this is like saying you have a letter but the address on it is blank.
If there is function that receive something as pointer, How do i pass an argument to it? For example i want to send char * word as parameter, How do i do it?
Such a function definition might look like this:
void swap(int* a, int* b)
// swap *a with *b
This function requires a pointer to an
int. There are two ways to pass the variable to the function, depending on the type of the source variable. If the variable being passed in is a pointer, then it can be used as-is, because it is already the same type:
int* a = NULL;
int* b = NULL;
// allocate memory for a and b, and assign values
However, if the variable is not a pointer, but instead a local stack variable, then it first needs to be converted to a pointer, using the "address-of" operator
int a = 7;
int b = 3;