Question for a pointer code fragment

  • Thread starter Linda8888
  • Start date
  • Tags
    Code
In summary, the conversation discusses the concept of casting in C programming and how it can be used to redefine pointers to specific data types. The conversation also mentions the use of the malloc function and the importance of including code in code tags for easier copying and pasting. The conversation also briefly touches on how memory addresses are assigned by the operating system.
  • #1
Linda8888
7
1
TL;DR Summary
Assume that the function print_mem_addr(void *) takes in a pointer and prints the machine address the pointer points to as an integer value (not hexadecimal). You may use the fact that sizeof(MyType*) is 8 and sizeof(MyType) is 16. What is the value of output (A), (B) and (C)?
(I am not sure what does "(MyType **)" means after the '=' sign at the second line)

截圖 2021-02-27 下午3.51.57.png
The output is :

截圖 2021-02-27 下午3.52.14.png
 
Technology news on Phys.org
  • #2
It's called a 'cast'. The malloc function returns a pointer of type void*. The (MyType **) redefines the pointer to be a pointer to a pointer to a structure of type MyType. It's still a pointer (a location in memory), it's just more narrowly defined. See the link below. By the way, if you include your code in the code tags (like this above </>), then it is easy to copy and paste the code. This is better than including a picture of the code.

https://www.tutorialspoint.com/cpro...o another,as follows − (type_name) expression
 
  • Informative
Likes berkeman
  • #3
phyzguy said:
It's called a 'cast'. The malloc function returns a pointer of type void*. The (MyType **) redefines the pointer to be a pointer to a pointer to a structure of type MyType. It's still a pointer (a location in memory), it's just more narrowly defined. See the link below. By the way, if you include your code in the code tags (like this above </>), then it is easy to copy and paste the code. This is better than including a picture of the code.

https://www.tutorialspoint.com/cprogramming/c_type_casting.htm#:~:text=Converting one datatype into another,as follows − (type_name) expression

Thanks for replying!
Why are the first output 432 and the third 1024? How do those work?
 
  • #4
Linda8888 said:


Thanks for replying!
Why are the first output 432 and the third 1024? How do those work?
Those are the memory addresses for, respectively, arr + 3 and *arr. The code you posted first allocates enough space on the heap for 10 pointers (addresses), and then iterates 10 times to fill those 10 addresses with the addresses of chunks of heap memory.
 
  • #5
Linda8888 said:
Thanks for replying!
Why are the first output 432 and the third 1024? How do those work?
The operating system puts those pointers wherever it finds space available in memory. In a real system, if you ran the program multiple times, those values might be different each time. So the exact values of those pointers is not the point. Given those values, you are supposed to calculate A, B, and C.
 

Related to Question for a pointer code fragment

1. What is a pointer in coding?

A pointer is a variable that holds the memory address of another variable. It allows a program to directly access and manipulate the value stored in that memory address.

2. How do I declare a pointer in my code?

To declare a pointer in your code, you use an asterisk (*) before the name of the variable. For example: int *ptr;

3. How do I assign a value to a pointer?

To assign a value to a pointer, you use the address-of operator (&) followed by the variable you want to point to. For example: ptr = # where num is the variable you want to point to.

4. How do I access the value of a pointer?

To access the value of a pointer, you use the dereference operator (*) before the pointer variable. For example: *ptr will give you the value stored at the memory address pointed to by ptr.

5. What is the purpose of using pointers in coding?

Pointers are commonly used in coding to optimize memory usage and improve efficiency. They allow for more complex data structures and dynamic memory allocation, which can be useful in certain applications.

Similar threads

  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
14
Views
501
  • Programming and Computer Science
Replies
2
Views
826
  • Programming and Computer Science
Replies
19
Views
1K
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
Back
Top