d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Easy C Question
12Next
Add Reply New Topic New Poll
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Sep 18 2012 07:04pm
Can someone explain what the * and & keys do? I'm used to java and don't really know much about the C syntax yet.

So for the following code, why are they included?

Code
int main() {
  int age = 41, capacity = 5;
int *age_ptr = &age;
int *p, *r;
int **q = &p;
   
  p = age_ptr;
r = &capacity;
printf("%d\n", *age_ptr);
  printf("%p\n", age_ptr);
  printf("%d\n", *p);
  printf("%d\n", **q);
  printf("%d\n", *r);
  q = &r;
printf("%d\n", **q);
  return 0;
}


Does it have anything to do with global and local variables or..?

This post was edited by lopelurag on Sep 18 2012 07:05pm
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Sep 18 2012 07:25pm
Ok so I guess the * indicates the a variable is a pointer?

Whats the point of a pointer if you can just delcare a variable like: int number = 0;
Member
Posts: 5,641
Joined: Apr 13 2006
Gold: 2.00
Sep 18 2012 07:26pm
* = dereference - http://stackoverflow.com/questions/4955198/what-does-dereferencing-a-pointer-mean
& = memory address, also mentioned/noted in the link above.
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Sep 18 2012 07:34pm
read any intro to C book or website or tutorial. they'll explain pointers and what they are, and why they're used.
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Sep 18 2012 07:37pm
Quote (SilverMice @ Sep 18 2012 09:26pm)
* = dereference -  http://stackoverflow.com/questions/4955198/what-does-dereferencing-a-pointer-mean
& = memory address, also mentioned/noted in the link above.


That helped thanks, just one question, whats the point of a pointer in the first place?

I'm used to java and don't exactly understand the point of a pointer? So it allocated a space in memory for you to store whatever data.

Like why not just do:

Code
int x = 5;
//using a pointer?
int x;
int *y = &x;
*y=5;



Quote (irimi @ Sep 18 2012 09:34pm)
read any intro to C book or website or tutorial.  they'll explain pointers and what they are, and why they're used.


Yeah that's what I'm doing right now

This post was edited by lopelurag on Sep 18 2012 07:38pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 18 2012 07:39pm
Quote (lopelurag @ Sep 18 2012 08:37pm)
That helped thanks, just one question, whats the point of a pointer in the first place?

I'm used to java and don't exactly understand the point of a pointer? So it allocated a space in memory for you to store whatever data.

Like why not just do:

Code
int x = 5;
//using a pointer?
int x;
int *y = &x;
*y=5;





Yeah that's what I'm doing right now


Pointers are incredibly useful. Just one example would be linked lists etc.
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Sep 18 2012 07:49pm
Pointers are used in Java as well... they're just hidden/abstracted away in the language. In fact, when you declare something like int x = 5; in Java, you are declaring a pointer.

This post was edited by irimi on Sep 18 2012 07:51pm
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Sep 18 2012 07:50pm
Quote (Eep @ Sep 18 2012 09:39pm)
Pointers are incredibly useful. Just one example would be linked lists etc.


Yeah ok I can see how they'd be important with linked lists, we just started learning about them.

In all the examples we've been shown we haven't really delved into WHY they are useful, just how to reference and dereference them.

So they just point to a location allocated in memory?
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 18 2012 07:51pm
Quote (lopelurag @ Sep 18 2012 08:50pm)
Yeah ok I can see how they'd be important with linked lists, we just started learning about them.

In all the examples we've been shown we haven't really delved into WHY they are useful, just how to reference and dereference them.

So they just point to a location allocated in memory?


Could be the class you are in. Unless you are utilizing/learning something which NEEDS pointers, they obviously won't seem important at the moment :p

just wait a bit

edit:

pointers contain the address (location) of some kind of data type. (in memory yes)


Could be an int, could even be a struct.

By deref, you can access the value of the address that the pointer points to.

This post was edited by Eep on Sep 18 2012 07:54pm
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Sep 18 2012 08:00pm
Quote (Eep @ Sep 18 2012 09:51pm)
Could be the class you are in. Unless you are utilizing/learning something which NEEDS pointers, they obviously won't seem important at the moment :p

just wait a bit

edit:

pointers contain the address (location) of some kind of data type. (in memory yes)


Could be an int, could even be a struct.

By deref, you can access the value of the address that the pointer points to.


Yeah I guess we just haven't really used them in a meaningful way. Thanks for the help I appreciate it.
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll