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.

Answer from StoryTeller - Unslander Monica on Stack Overflow
Top answer
1 of 3
1

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

2 of 3
1

rioShardMemoryWindowGet should accept uint8_t **baseAddrPtr if you want it to modify baseAddr0. Then you'll have *baseAddr = sharedMemory without a cast.

Discussions

How do I copy the address of a pointer into a character array in c?
What's the purpose of copying it into the char array? Do you want it in the form of a string that you could print? If so it will be something like this: int* myPointer = ...; char myString[100]; sprintf( myString, "%p", myPointer ); That will convert the address that myPointer points to into a string. More on reddit.com
๐ŸŒ r/NoStupidQuestions
5
1
September 19, 2021
c - Copy memory address to a memory block - Stack Overflow
I haven't been programming in C for a very long time I and have recently decided to come back to it. I'm posting this question because there's something I can't fully understand about the memcpy function. I'd like to be able to copy memory addresses into a block of memory. More on stackoverflow.com
๐ŸŒ stackoverflow.com
November 11, 2020
C how to copy by value? - Stack Overflow
@Torra network_addr and last_addr ... location in memory. ... @Torra You can print the memory addresses with printf("%p\n%p\n", (void *) &network_addr, (void *) &last_addr);. But I don't see how that might help you. ... I think your issue is not with references / copy by value (you ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do I copy the value not the address of a pointer in C here? - Stack Overflow
A journal chose a header image for my paper, should I push back for diversity reasons? ... For us to see an exoplanet transit, what is the maximum angle between that planet's orbital plane and our line of sight? What reasons do Mormons give for the usage of the name "Alma" for males? Industry standard for EC2 instance (server) shell access ... To subscribe to this RSS feed, copy ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Quora
quora.com โ€บ When-you-pass-an-address-to-a-function-in-C-does-the-function-always-make-a-local-copy-of-the-address
When you pass an address to a function in C, does the function always make a local copy of the address? - Quora
Technically, everything is passed ... element). What this means is that normally, a copy of the argument is placed on the call stack, where the called function then accesses it....
๐ŸŒ
Yale University
cs.yale.edu โ€บ homes โ€บ aspnes โ€บ pinewiki โ€บ C(2f)Pointers.html
C/Pointers
To initialize a pointer variable, you have to assign to it the address of something that already exists. Typically this is done using the & (address-of) operator: 1 int n; /* an int variable */ 2 int *p; /* a pointer to an int */ 3 4 p = &n; /* p now points to n */ Pointer variables can be ...
๐ŸŒ
HowStuffWorks
computer.howstuffworks.com โ€บ tech โ€บ computer software โ€บ programming
Pointers: Pointing to the Same Address - The Basics of C Programming | HowStuffWorks
March 8, 2023 - Note that in this code, r points to the same thing that p points to, which is i. You can assign pointers to one another, and the address is copied from the right-hand side to the left-hand side during the assignment.
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 138347-copy-pointer-address-buffer-convert-back.html
Copy pointer address to buffer and convert it back
May 26, 2011 - That string has two values - a length and address. If so, sprintf/sscanf is the way to go here. Assuming you control both programs the format is up to you so make it easy to parse. ... Now why would you want to go and dig up Cobol's grave? Can't you let that language die in peace?
๐ŸŒ
Florida State University
cs.fsu.edu โ€บ ~myers โ€บ cgs4406 โ€บ notes โ€บ pointers.html
Pointer Basics and Pass-By-Address
It contains some random value from memory right now, because we haven't initialized it. However, we can point p at n by using the & operator. p = &n; // assigns the "address of n" to the pointer p ยท We've seen Pass By Value and Pass By Reference. If you declare a formal parameter of a function as a pointer type, you are passing that parameter by its address. The pointer is copied, but not the data it points to.
Find elsewhere
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 64300154 โ€บ copy-memory-address-to-a-memory-block
c - Copy memory address to a memory block - Stack Overflow
November 11, 2020 - Or, based on most of the code you have, instead of that, do: struct Person** ptr = buffer; printf("person address: %p\tptr address: %p.\n", person, *ptr); (Note the double star pointer). ... I believe you must change the line struct Person* ptr = (struct Person*) buffer; to struct Person* ptr = *(struct Person**) buffer; This is because buffer points to a pointer which points to a struct Person. ... "This won't copy the address of person into buffer."
Top answer
1 of 1
1

If you have an array declared like

char* names[9];

then the valid range of indices for this array is [0, 9) .

So these statements

names[9] =  (char *) malloc(4 * sizeof(char*));
strcpy(names[9],"gib");

access the non-existent element of the array with invalid index 9.

This loop

for (i = 0; i < 10; i++) {
    free(names[i]);
} 

is also incorrect by the same reason.

Do not us magic numbers like 9 0r 10. Use named constants as for example

enum { N = 9 };
char* names[N];

//...

for (i = 0; i < N; i++) {
    free(names[i]);
} 

And there is no sense to declare the array names as a global variable. You could declare it in main.

Though the allocated memory

names[0] =  (char *) malloc(4 * sizeof(char*));

can accommodate a string of four characters

strcpy(names[0],"foo");

nevertheless such an allocation only confuses readers of the code. Instead write in this an other similar statements

names[0] =  (char *) malloc(4 * sizeof(char));
                                       ^^^^^

or just

names[0] =  (char *) malloc( 4 );

Th function diddle the second parameter has the type char *

void diddle(char *names[], char *name) {

but in the function call

diddle(&names[0],&name);

there is used expression &name of the type char **.

As a result this statement

strcpy(name,names[j]);

invokes undefined behavior. Even if an passed expression had correct type

diddle(&names[0],&name);

nevertheless the function again had undefined behavior because the passed pointer is a null pointer. So you may not use strcpy with a null pointer.

Instead of the null pointer

char *name = NULL;

you could use a character array like

char name[4];
๐ŸŒ
Quora
quora.com โ€บ How-is-a-value-physically-copied-from-one-memory-address-to-another-E-g-If-I-make-a-copy-of-a-variable-in-C-how-is-that-actually-implemented-in-hardware
How is a value physically copied from one memory address to another? E.g. If I make a copy of a variable in C, how is that actually implemented in hardware? - Quora
Answer (1 of 3): Well, the issue you are dealing with is the layers of abstraction that we have created in the programming languages to isolate ourselves from the utter banality of having to deal with the hardware at such a low level. If you really want to understand this then please look up ass...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c++ โ€บ memcpy-in-cc
memcpy() in C - GeeksforGeeks
September 22, 2025 - The memcpy() function makes a shallow copy as it only copies the raw bytes of the memory from one location to another. It does not perform a deep copy or handle objects at a higher level. memcpy() copies the actual bytes of the memory you specify.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ c pass addresses and pointers
C Pass Addresses and Pointers - Scaler Topics
October 5, 2023 - Each variable in memory in C has a unique address - a position in the computer's memory where its data is stored. These addresses are written in hexadecimal format, making them easy to identify. In C, you may provide a variable to a function in one of two ways: by a value or a reference. A copy of the variable's value is sent to the function when passing by value.