It's UWAweek 44 (2nd semester, 1st exam week)

help2002

This forum is provided to promote discussion amongst students enrolled in CITS2002 Systems Programming.
Please consider offering answers and suggestions to help other students! And if you fix a problem by following a suggestion here, it would be great if other interested students could see a short "Great, fixed it!"  followup message.
Displaying the 3 articles in this topic
Showing 3 of 820 articles.
Currently 8 other people reading this forum.


 UWA week 35 (2nd semester, week 6) ↓
SVG not supported

Login to reply

👍?
helpful

This isn't a question but just something I find useful and hopefully others might also find useful, to get my head around the different uses of the * operator in pointer syntax.

While it seems like the expression *p can take on a few different meanings depending on context, it can pretty consistently be read as "the value of p". I'll illustrate with some examples (compared against regular/non-pointer equivalents):

int x;  // define x to be an int
int *p; // define the value of p to be an int

x = 5;  // set x to 5
*p = 5; // set the value of p to 5

int y;
y = x;  // set y to x
y = *p; // set y to the value of p

It even works for functions, kinda:

int foo()  // the thing foo returns will be an int
{
    ...
}

int *foo() // the value of the thing foo returns will be an int
{
    ...
}

It's not perfect obviously, but it's maybe a useful fallback and preferable to trying to remember how each of the contexts work independently.


SVG not supported

Login to reply

👍?
helpful

If it is an operator, that is referring to the value of a variable storing a specific address, so the content can be read or written. If it is an specifier, that is denoting the date type is a pointer to something, like a pointer to integer, or even a pointer to a pointer. The tricky part is that these two usages both use '*'. As we cannot dereferencing the value when declaring itself, like variables and return types, nor can we change the data type to pointer when dereferencing it, these usages will not have any ambiguity when we use any of them. Just need some time to get used to them.


SVG not supported

Login to reply

👍?
helpful

Hi, the way I remember it is that * refers to a pointer if a datatype comes before it e.g. int*. If there is no type beforehand, then * is the dereference operation.

e.g. int* p, declares a pointer to an int. *p refers to the value stored at p.

Works for function definitions

int* somefunction(char* string)

Function takes in a pointer to a character and returns a pointer to an int.

Another helpful way to think of pointers is to think about houses:

  • If you have a house, you can write down its address (using address = &house).
  • If you follow an address, you can follow it to get to the house (using house = *address).
  • You can write down the address to the same house multiple times.
  • If you want work done on your house you need to pass your address.
  • Just because two houses look the same doesn't mean they have the same address.

The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Written by [email protected]
Powered by history
Feedback always welcome - it makes our software better!
Last modified  8:08AM Aug 25 2024
Privacy policy