
Arrays and pointers are core topics in C/C++ because they directly connect to how memory works.
Understanding them early makes later OOP topics (dynamic objects, references, data structures) easier.
An array is a collection of elements of the same data type stored in contiguous memory locations.
Key properties:
Example: int a[5]; stores 5 integers.
int a[5];int a[5] = {1,2,3,4,5};int a[5] = {1,2}; (remaining become 0)Use a loop:
for (int i=0; i<5; i++) cout << a[i];A 2D array is like a table (rows × columns).
Example: int m[3][4];
Access element:
m[i][j]Memory idea (simple):
Typical tasks:
In exams, always mention:
There are two common string forms:
\0char name[20] = "Amar";string s = "Hello";For Sem 1 exams, you must understand C-style strings basics because they are pointer-related.
A C-style string ends with null character \0 which marks the end.
Access the complete note and unlock all topic-wise content
It's free and takes just 5 seconds
Download this note as PDF at no cost
If any AD appears on download click please wait for 30sec till it gets completed and then close it, you will be redirected to pdf/ppt notes page.
Arrays and pointers are core topics in C/C++ because they directly connect to how memory works.
Understanding them early makes later OOP topics (dynamic objects, references, data structures) easier.
An array is a collection of elements of the same data type stored in contiguous memory locations.
Key properties:
Example: int a[5]; stores 5 integers.
int a[5];int a[5] = {1,2,3,4,5};int a[5] = {1,2}; (remaining become 0)Use a loop:
for (int i=0; i<5; i++) cout << a[i];A 2D array is like a table (rows × columns).
Example: int m[3][4];
Access element:
m[i][j]Memory idea (simple):
Typical tasks:
In exams, always mention:
There are two common string forms:
\0char name[20] = "Amar";string s = "Hello";For Sem 1 exams, you must understand C-style strings basics because they are pointer-related.
A C-style string ends with null character \0 which marks the end.
Common string functions (from <cstring>):
strlen(s) → lengthstrcpy(dest, src) → copystrcat(dest, src) → concatenatestrcmp(a,b) → compareImportant note: these functions assume proper null-terminated arrays and enough space.
A pointer is a variable that stores the address of another variable.
Declaration:
int *p; → pointer to intInitialization:
int x = 10; int *p = &x;&x gives address of x.*p gives the value stored at address in p (dereference).Example:
p = &x, then *p is the value of x.Pointer arithmetic means moving the pointer to the next/previous memory location of the same type.
If p is int*:
p + 1 moves by sizeof(int) bytes (not by 1 byte).This is why pointer arithmetic is closely related to arrays.
Key idea:
So:
a is like &a[0]*(a + i) is same as a[i]This equivalence is frequently asked.
Static arrays have fixed size known at compile time. Sometimes we need memory at runtime.
Dynamic memory allocation allows:
In C++:
new allocates memorydelete releases memoryExample:
int *p = new int; (one integer)delete p;For arrays:
int *a = new int[n];delete[] a;Important rule: use delete[] for memory allocated with new[].
These are high-scoring short notes.
To avoid:
nullptr)nullptrDecide size at runtime → new/new[] → use memory → delete/delete[] → set pointer nullptr
strlen/strcpy/strcmp are common.& gets address and * gets value.sizeof(type).new/delete (and new[]/delete[]).Get instant access to notes, practice questions, and more benefits with our mobile app.
From this topic
A 1D array stores multiple values of the same type.
Declaration: int a[5];
Initialization: int a[5]={1,2,3,4,5};
Traversal uses a loop: for(i=0;i<5;i++) cout<<a[i];
Because elements are contiguous, indexing is fast.
C-style string is a char array ending with '\0' and often uses <cstring> functions like strlen.
std::string is a C++ class that manages memory automatically and supports easy operations like concatenation and length using methods.
C-style strings need careful size handling; std::string is safer.
A pointer is a variable that stores the address of another variable.
Key operators:
&): gives the address of a variable.*): gives the value stored at that address.Example idea:
int x = 10;int *p = &x; (p stores address of x)*p gives 10x (value=10) stored at some address, say 1000
p stores 1000
*p reads value at 1000 → 10
Thus pointers help programs access and manipulate memory efficiently.