It is the same with pointers. All variables in C are passed by value, even pointers.
You copy the address stored in the pointer outside the function, into its parameter.
But you can use that address to reference a variable which can be allocated anywhere. So in the following code:
int j = 0;
incVar(&j);
incVar receives by-copy the address of j. But it can use that address to read or modify j (in)directly.
It is the same with pointers. All variables in C are passed by value, even pointers.
You copy the address stored in the pointer outside the function, into its parameter.
But you can use that address to reference a variable which can be allocated anywhere. So in the following code:
int j = 0;
incVar(&j);
incVar receives by-copy the address of j. But it can use that address to read or modify j (in)directly.
In fact the same thing as with values happens with pointers. Just make sure to understand the syntax correctly. You are not passing the integer *i by copy to incVar, but you are passing the pointer i of type int* by copy. No matter how often you copy the pointer to an address, it always points to the same address. So i in your second incVar example points to the integer the caller took the address of. So by derefencing the copy of the pointer (in (*i)++), you are acessing the integer of the caller.
Ok I think I understand your problem.
You're trying to store the address number in baseAddr0, am I right? (not sure the reasons but this is the only thing I came up with).
The reason that a 0x500000 is showing as a 0 is because a uint8_t has not enough bits to represent an address and so it's "culling it down" to only 1 byte (therefore showing a 0).
Change the baseAddr to a uint32_t and voila, everything works.
Anyways, the reason the other posters are telling you to use a pointer to pointer is because what you seem to be doing is weird, unless you're planning on using the address for something special such as displaying it or using as an offset, perhaps?
p.s.: you're also going to need to change this line
*baseAddr=(uint32_t)sharedMemory;
edit: your code should look like this to get what you want:
/* Window size in bytes. */
static uint32_t size = 0;
/* Window address. */
static uint32_t address = 0;
/* Memory Base Address */
static uint8_t *sharedMemory=NULL;
sharedMemory = memalign(size, size);
void rioShardMemoryWindowGet (uint32_t *baseAddr,uint32_t *memorySize,uint32_t *windowAddress )
{
*baseAddr=(uint32_t)sharedMemory;
printf("sharedMemory: #%x",sharedMemory);
*memorySize=size;
*windowAddress=address;
}
rioShardMemoryWindowGet(&baseAddr0, &baseSize, &(Addrs.virtualBaseAddr));
printf("baseAddr0 : #%x",baseAddr0);
The reason why you NEED an uint32 to store the numeric address is because addresses are 32 bits, and that's the reason why you see a 0 using an 8 bit value, because 0x500000 maps to 0x00 to a byte
rioShardMemoryWindowGet should accept uint8_t **baseAddrPtr if you want it to modify baseAddr0. Then you'll have *baseAddr = sharedMemory without a cast.
How do I copy the address of a pointer into a character array in c?
c - Copy memory address to a memory block - Stack Overflow
C how to copy by value? - Stack Overflow
How do I copy the value not the address of a pointer in C here? - Stack Overflow
Repeat the ADDRESS, not the value stored at the memory location..
I tried asking the all-knowing Google but it tries to be very helpful and shows me how to copy the value that stored in the pointer into a string...
I think your issue is not with references / copy by value (you are already copying by value). It seems to me like you messed up on network / host byte order.
The sa_addr field of struct in_addr is in network byte order; when you copy that to network_addr and last_addr, you should convert it to host byte order, since you'll be doing shifting and other manipulations.
Furthermore, for some reason the code assumes that netmask is in network byte order, whereas in fact it is in host byte order. Things do not add up.
Just stick to host byte order and everything should work.
Change this line:
network_addr = last_addr = addr.s_addr;
To:
network_addr = last_addr = ntohl(addr.s_addr);
And delete this line:
netmask = ntohl(netmask);
You probably want to add return network_addr; in the end and possibly change the return type to uint32_t.
You may need to malloc your new variables, then memcpy the old variable to your new variables.
The moment you do bb = first;, bb and first are pointing to the same location of memory. first->a = 55; first->b = 55; first->c = 89; will change the values for a, b, and c in that location. The original value of first, is still lingering in memory but no way to access it anymore.
I think what you may want to do is *bb = *first;.
Your knowledge about memcpy is correct but you cannot assign contents of "location pointed to by the pointers" just by assigning pointers like you did in your statement mentioned above.
You are assigning one pointer to the another in the following statement:
bb = first;
Now both these point to the same memory location (think of bb as an alias to first).
If you want to copy data then you copy using "data pointed to by pointers" *bb = *first
your first example will reserve memory on the stack for the variable copy.
it will then copy the content of now to the new memory-location, and finally have next point to that memory location.
tm copy = *now;
tm* next = ©
the second example will not reserve any new memory; instead it will simply assign next the address (&) of the object now points to (*).
tm* next = &(*now);
the reason they are different, is that C never does any "automagic" memory allocation. you get what you ask for: if you ask for memory for a variable (as in the first example) you get it; if you don't ask for it, you don't get it.
so the difference between the two examples is really that declaring a variable (tm copy) is asking for memory.
= is only for copying data from one memory location to another. & only gives you address of a variable. * only dereferences a variable. None of these allocate memory in any form, or create any temporary variables.
All memory allocations must be explicitly stated in C. There is no difference to be found here.
You want to use %p to print a pointer. From the spec:
pThe argument shall be a pointer tovoid. The value of the pointer is converted to a sequence of printing characters, in an implementation-defined manner.
And don't forget the cast, e.g.
printf("%p\n",(void*)&a);
When you intend to print the memory address of any variable or a pointer, using %d won't do the job and will cause some compilation errors, because you're trying to print out a number instead of an address, and even if it does work, you'd have an intent error, because a memory address is not a number. the value 0xbfc0d878 is surely not a number, but an address.
What you should use is %p. e.g.,
#include<stdio.h>
int main(void) {
int a;
a = 5;
printf("The memory address of a is: %p\n", (void*) &a);
return 0;
}
Good luck!
You should use *variable to refer to what a pointer points to:
*x = 5;
*y = 5;
What you are currently doing is to set the pointer to address 5. You may get away with crappy old compilers, but a good compiler will detect a type mismatch in assigning an int to an int* variable and will not let you do it without an explicit cast.
void function(int *x, int* y) {
*x = 5;
*y = 5;
}
would change the values of the parameters.
Given any variable in C, you can get its address using the "address-of" operator &:
int x;
int* addressOfX = &x;
You can print out addresses using the %p specifier in printf:
printf("%p\n", &x); // Print address of x
To access individual bits of an integer value, you can use the bitwise shifting operators, along with bitwise AND, to shift the bit you want to the proper position and then to mask out the other bits. For example, to get the 5th bit of x, you can write
int x;
int fifthBit = (x >> 4) & 0x1;
This shifts down the number 4 bits, leaving the fifth bit in the LSB spot. ANDing this value with 1 (which has a 1 bit in the lowest spot and 0 bits everywhere else) masks out the other bits and returns the value. For example:
int x = 31; // 11111
prtinf("%d\n", (x >> 4) & 0x1); // Prints 1
This worked for me ;)
// uses :: head
// ----------------------------------------------
#include <stdio.h>
// ----------------------------------------------
// func :: main
// ----------------------------------------------
int main()
{
void *fooz = "bar";
char addr[64];
sprintf(addr, "%p", &fooz);
// do some stuff with `addr`, or not :)
puts(addr);
return 0;
}
// ----------------------------------------------
Prints out something like: ~> 0x7ffdfb91d698
void* memAddr = (void*)0xAABBCCDD; //put your memory address here
printf("int value at memory address %p is %i", memAddr, *((int*)memAddr));
You'll probably find that examining arbitrary memory addresses will just cause a crash.
That would be pointer dereferencing, which is the * operator. However, you have no way of telling what the type of the stored data is for a random address.
#include <stdio.h>
int main(void) {
int x = 0;
printf("\n%d\n%d\n%d\n",x,&x,*(&x));
}
/* resulting output will be 0, some memory address, and 0 again */