Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Difference between types and references

variable type

  • Please log in to reply
6 replies to this topic

#1 DreamFM

DreamFM

    CC Newcomer

  • Member
  • PipPip
  • 13 posts

Posted 22 July 2012 - 10:14 PM

typedef struct{
			    char name[10];
			    double diameter,orbit_time, rotation_time;
			    int moons;
}planet_t;

status = scan_planet(&current_planet);
int scan_planet(planet_t *plnp){
				    int result;
				    result= scanf("%s",(*plnp).name);
		    if(result==1)
				    result=1;
		    else
				    result =0;
return(result);
}


reference Type value;
plnp planet_t * address of structure that main refers to as current_planet

*plnp planet_t structure that main refers to as current_planet

i don understand the reference part and the type part? What does it mean?
For a pointer, the * sign belongs to the data type(for example int,char) or the variable type (the identifier)?
and is this function filled up with input or output argument? Whatever the case, what is the difference between them?
and the * in *plnp in the body of the function is the deference point right?

Sorry for the noob questions, i am self learning and kinda of getting confused here with pointer, and pointer as argument.
thanks
  • 0

#2 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 23 July 2012 - 12:47 AM

If you want to declare a function which will take a 'pointer of type integer', you need to declare like
void aFunction(int* ptr)
{
*ptr = 0;
}
, and when you will call that function, you need to call like this
//example 1
int iVar = 0; // integer type
aFunction(&iVar);// here using & operator which reference the address of a variable.

//example 2
int* iPtr; // pointer to integer.
aFunction(iPtr);
.

In your case, the type is planet_t in place of int. Yes, *ptr (in your case *plnp) differences the pointer.

So when you declare a variable and use * after type, it will declare a pointer variable for that type and when you use the * operator before a pointer variable, it de-references that pointer to the variable it contains.
  • 0

#3 DreamFM

DreamFM

    CC Newcomer

  • Member
  • PipPip
  • 13 posts

Posted 23 July 2012 - 12:57 AM

Thanks, you have been really helpful!

oh yupp, how about the difference between input and output variable, i cant seem to differentiate them..
  • 0

#4 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 23 July 2012 - 12:58 AM

What do you mean by input & output variables?
  • 0

#5 DreamFM

DreamFM

    CC Newcomer

  • Member
  • PipPip
  • 13 posts

Posted 23 July 2012 - 01:00 AM

sorry, what i meant was input and output argument.

for example, the struct argument i put in the scan_planet function, is it a input argument or an output argument ?
  • 0

#6 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 23 July 2012 - 01:06 AM

Well, there is nothing like input or output argument in C or C++. What you need to know is about 'Pointer'. Start by reading some simple tutorials on pointer. You can start with this one.
  • 0

#7 DreamFM

DreamFM

    CC Newcomer

  • Member
  • PipPip
  • 13 posts

Posted 23 July 2012 - 01:39 AM

okay, thanks alot! (:

Happy coding!
  • 0





Also tagged with one or more of these keywords: variable type

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download