I've used the two terms interchangably since they do the same thing, in the end.
I suppose you could say a pointer, is a kind of variable which stores addresses.
You are correct that you don't have to dereference a function pointer, but they're special for a reason. Functions are addresses, just like arrays are -- arrays point to data memory, functions point to code memory. The program calls a function by its address. Try this: printf("%p\n", printf); I think C even lets you take the address of main, but C++ doesn't.
You definitely do have to use * for pointers to structures! Either that or the -> operator, which is just a short-form thing which does two things at once. These two statements are equivalent: (*structptr).member vs structptr->member
The same applies to classes, which are just a refinement of structures.
Last edited by Corona688; 01-21-2014 at 11:45 AM..
While I was reading more about pointer in C, I came up another silly question for the same "call-by-reference swap example" here.
I thought Line 23 is to assign the "address ofx" with "the address ofy" and then Line 24, makes y pointing to t, so that the addresses exchanged. Why my understanding is wrong?
Or, what is the difference between "x = y" and "*x = *y" from memory perspective? Thanks a lot!
No! The way to read the statements in:
is:
Set the contents of t to be the integer pointed to by the contents of x.
Set the contents of the integer pointed to by the contents of x to be the integer stored in the location pointed to by the contents of y.
Set the contents of the integer pointed to by the contents of y to be the integer stored in t.
This User Gave Thanks to Don Cragun For This Post:
Thanks Don!
Good I thought the same way on your swap1() function as you interpreted!
Then how is my way read?
The reason I posted here is there was no error or warning, and it did not work the way I supposed. I must have missed important thing.
Thanks Don!
Good I thought the same way on your swap1() function as you interpreted!
Then how is my way read?
The reason I posted here is there was no error or warning, and it did not work the way I supposed. I must have missed important thing.
Imagine that x and y are just integers. What, exactly, will these two statements cause to happen outside the function?
If you said 'absolutely nothing', you are correct... The integers are passed by value, changing their value does not change anything outside the function.
Now switch them out for pointers again... What changes? Once again, absolutely nothing. You changed the values of X and Y, not their contents. Pointers are just fancy integers. Assigning something to a pointer does not magically change its contents.
Last edited by Corona688; 02-20-2014 at 11:57 AM..
Thanks, I think this is the part I am not clear about: You changed the values of X and Y, not their contents.
Does that imply if I want to change the contents of pointers X and Y, only expression *X = *Y must be used? Please confirm this, thank you!
When I checked the swap function, it seems x and y got swapped, but not a and b.
I have a strong feeling but very vague idea of the stack/heap for this swap-function calling in the memory, especially when the code did not work without an error nor any warning. Thanks a lot!
Hi Solaris Folks :),
I need to calculate the swap usage on solaris server, please let me understand the output of below swap -s and swap -l commands.
$swap -s
total: 1774912k bytes allocated + 240616k reserved = 2015528k used, 14542512k available
$swap -l
swapfile dev swaplo... (6 Replies)
Guys,
May i know how can we de reference the code reference variable.?
my $a = sub{$a=shift;$b=shift;print "SUM:",($a+$b),"\n";};
print $a->(4,5);
How can we print the whole function ?
Please suggest me regarding this.
Thanks for your time :)
Cheers,
Ranga :) (0 Replies)
Hi All,
I have just installed my first Linux server ( Ubuntu 11.10 ).
I am sure I didn't allocate /swap , and double check by 'df -h', yes really no /swap
but when I use 'free -m' , returned a "Swap" line as below.
total used free shared buffers cached
Mem: ... (3 Replies)
Ive just started programming in C and am confused with the output of the code
void main()
{
int a=10;
int b=20;
swapr(a++,b++);
printf("%d %d",a,b);
}
swapr (int *x,int *y)
{
x+=2;
y+=3;
}
the output of the code is 11,21 which is confusing. first its... (3 Replies)
Hi
I have an integrity machine rx7620 and rx8640 running hp-ux 11.31. I'm planning to fine tune the system:
- I would like to know when does the memory swap space spill over to the device swap space?
- And how much % of memory swap utilization should be specified (swap space device... (6 Replies)
Hello,
I have a problem with package and name space.
require "/Mehran/DSGateEngineLib/general.pl";
use strict;
sub System_Status_Main_Service_Status_Intrusion_Prevention
{
my %idpstatus;
my @result;
&General_ReadHash("/var/dsg/idp/settings",\%idpstatus);
#print... (4 Replies)
Hello and thanks in advance.
I have a Sun box with raid 1 on the O/S disks using solaris svm.
I want to unmirror my swap partition, and add the slice on the second disk as an additional swap device. This would give me twice as much swap space.
I have been warned not to do this by some... (3 Replies)
Hi Folks,
This is my first post here - so nice to meet u all :-)
Recently i was trying to dig a little bit into the memory structure of my machine and due to the lack of concept, cannot figure out a calculation.
This is how it goes:
1) My swap slice is at the usual /dev/dsk/c0t1d0s1... (0 Replies)