Pointer vs Array in C https://www.skillvertex.com/blog Fri, 10 May 2024 11:22:12 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 https://www.skillvertex.com/blog/wp-content/uploads/2024/01/favicon.png Pointer vs Array in C https://www.skillvertex.com/blog 32 32 Pointer Vs Array In C https://www.skillvertex.com/blog/pointer-vs-array-in-c/ https://www.skillvertex.com/blog/pointer-vs-array-in-c/#respond Fri, 10 May 2024 11:22:12 +0000 https://www.skillvertex.com/blog/?p=2733 Read more]]>

Table of Contents

Pointer Vs Array In C

1. The sizeof Operator

When you use sizeof(array), it tells you the size (in bytes) that the entire array occupies in memory. For example, if you have an array int arr[5], sizeof(arr) will return the total memory consumed by all five integers in the array.

On the other hand, sizeof(pointer) only gives you the size of the pointer variable itself, which is typically a fixed amount of memory (e.g., 4 or 8 bytes, depending on your system). It doesn’t account for the memory occupied by what the pointer is pointing to; it just tells you the size of the “address holder.”

2. The & Operator

When you use array, it’s like saying “give me the address of the first element in the array.” So, if you have an array int arr[5], array is equivalent to &arr[0] and provides the memory address of the first integer in the array.

When you use &pointer, it gives you the memory address where the pointer variable is stored, not what it’s pointing to. It’s like asking for the address of the “address holder” itself.

3. String Literal Initialization of a Character Array

When you initialize a character array-like char str[] = "Hello";, it creates an array named str. This array is large enough to hold the string “Hello” along with a null-terminator character ('\0') at the end, which signifies the end of the string. So, str contains space for 6 characters (5 for “Hello” plus 1 for '\0').

This is different from a pointer to a string literal. If you had char* ptr = "Hello";, ptr would point to a constant string literal in memory, and its size is just the size of a pointer (e.g., 4 or 8 bytes), not the size of the string itself.

4. Pointer variable can be assigned a value whereas an array variable cannot be

In C and C++, you can assign a value to a pointer variable, as shown in your example with p=a;. However, you cannot directly assign one array to another, as arrays are not modifiable l-values.

5. Arithmetic on pointer variable is allowed

You can perform arithmetic operations on pointer variables, such as incrementing or decrementing them, which is often used to navigate through arrays or data structures. In your example, p++; is legal because it increments the pointer p to point to the next element.

6. Arithmetic on array variable is not allowed

You cannot perform arithmetic operations directly on array variables. Arrays are not modifiable l-values, so a++; is illegal because you’re trying to modify the base address of the array.

7. Array is a collection of similar data types while the pointer variable stores the address of another variable

Arrays are collections of elements of the same data type. Pointers, on the other hand, store memory addresses that can point to variables or data of various types. Pointers provide flexibility in referencing different data types.

Understanding these distinctions is important for writing code that correctly handles memory and data in C and C++.

FAQ-Pointer vs Array in C

Q1. What is the difference between a pointer and an array in C?

Ans.
Array: A data collection with elements of the same data type.
Pointer: A variable that stores the memory address of another variable.

Q2. Why is pointer better than array?

Ans.
Arrays: Static; their size can’t change based on user needs.
Pointers: Dynamic; memory allocation can be resized later if required.

Q3. What is the size of array vs pointer?

Ans.
Arrays: Always have a set, unchanging amount of memory.
Pointers: Can adjust the amount of memory they use as needed.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

]]>
https://www.skillvertex.com/blog/pointer-vs-array-in-c/feed/ 0