Question:
int vs *int in c program..when to use what?
That's what she said
2010-11-22 18:33:42 UTC
Hmm, another c question just popped into my head that I have wondered about for a while...

When do use int n?
and when do you use int *n? (i know this is a pointer to an integer)

I have a struct, and inside will have an integer value, which should I use?? should I just ALWAYS use int *n?

Thanks!
Three answers:
oops
2010-11-22 18:41:21 UTC
You should of course, just use an int. You would only use an int* if you needed a dynamic array of ints, or if you were using it as a reference to another int.
a-nerd
2010-11-22 18:40:30 UTC
int* is a pointer value that can be decoded to a memory location of an int

int is an integer value

*x, assuming x is a pointer variable, gets the value at the memory location that x represents.



for example,

void add(int *n, int a);

n can be changed, but a can't.

You should use int *n in a struct if you want the value of the int pointed to by n to be changed with the struct itself.

You should use int n in a struct if you want the struct to have an int specific to it.

I think it is better to use int n in a struct for organizational reasons, and to pass a pointer to the struct around as an argument.
arnoult
2016-10-17 13:07:37 UTC
confident. int significant() returns a 0 whilst this device ran effectively. in case you call your software from the cmd line or batch report or what ever, you could examine againt the return fee. in case your significant has void, then you isn't waiting to envision against a valid run.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...