Understanding the Difference between int and int* Pointers

  • Thread starter Thread starter transgalactic
  • Start date Start date
Click For Summary
SUMMARY

The discussion clarifies the distinction between pointer declarations in C/C++. When declaring int* ptr_a, ptr_b;, only ptr_a is a pointer to an integer, while ptr_b is simply an integer. This is due to the syntax of C/C++, where the asterisk (*) applies only to the variable immediately preceding it, leading to the conclusion that int *ptr_a, ptr_b; translates to int *ptr_a; and int ptr_b;.

PREREQUISITES
  • Understanding of C/C++ syntax and variable declarations
  • Knowledge of pointer concepts in programming
  • Familiarity with data types in C/C++
  • Basic debugging skills in C/C++ environments
NEXT STEPS
  • Research the rules of pointer arithmetic in C/C++
  • Learn about memory management and dynamic allocation using malloc and free
  • Explore the differences between int* and int& in C++
  • Study best practices for using pointers to avoid memory leaks and segmentation faults
USEFUL FOR

Programmers, computer science students, and software developers seeking to deepen their understanding of pointers and memory management in C/C++.

transgalactic
Messages
1,386
Reaction score
0
when i declare

Code:
int* ptr_a, ptr_b;

why the type of ptr_b is int
and not int*

?
 
Technology news on Phys.org
Because
Code:
int *pta_a, ptr_b;
means
Code:
int *pta_a; int ptr_b;
and not
Code:
int *pta_a; int *ptr_b;
which would be
Code:
int *pta_a, *pta_b;
.
 
thanks
 

Similar threads

  • · Replies 17 ·
Replies
17
Views
3K
Replies
3
Views
2K
Replies
7
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 13 ·
Replies
13
Views
1K
Replies
5
Views
2K
  • · Replies 22 ·
Replies
22
Views
4K
Replies
12
Views
2K
  • · Replies 23 ·
Replies
23
Views
2K