Programming Articles - Page 2747 of 3366

An Uncommon representation of array elements in C/C++

Nitya Raut
Updated on 30-Jul-2019 22:30:25

132 Views

This is a simple C++ program of an uncommon representation of array elements.#include using namespace std; int main() { int array[5] = {7,7,7, 6, 6}; for (int i = 0; i < 5; i++) cout

Why C treats array parameters as pointers?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

296 Views

C treats array parameter as pointers because it is less time consuming and more efficient. Though if we can pass the address of each element of the array to a function as argument but it will be more time consuming. So it’s better to pass the base address of first element to the function like:void fun(int a[]) { … } void fun(int *a) { //more efficient. ….. }Here is a sample code in C:#include void display1(int a[]) //printing the array content {    int i;    printf("Current content of the array is: ");    for(i = 0; i < ... Read More

How to create a dynamic array of integers in C++ using the new keyword

Farhan Muhamed
Updated on 02-Jun-2025 19:34:43

11K+ Views

Dynamic arrays are a type of array that can change their size when new elements are added or removed. They are created using pointers and memory management operators like new and delete. In this article, we will learn how to create and use a dynamic array in C++. What is Dynamic Array? A dynamic array is an array that can change its size during runtime. This is different from a static arrays, which have a fixed size determined by the programmer at compile time. Dynamic arrays are used when the size of the array is not known at compile ... Read More

Passing Arrays to Function in C++

Farhan Muhamed
Updated on 02-Jun-2025 19:35:02

394 Views

In this article, we will learn different ways to pass arrays to functions in C++ and how to work with them effectively. Passing Arrays to Functions Here are list of approaches to pass arrays to functions in C++ that will be discussed in this article along with examples: As a Sized Array As an Unsized Array As a Pointer (Pass by Pointer) As a Reference (Pass by Reference) As a Sized Array This method passes an array to a ... Read More

How to initialize a dynamic array in C++?

Farhan Muhamed
Updated on 16-Jul-2025 16:01:28

3K+ Views

Dynamic arrays are a type of array that can grow or shrink their size as elements are added or removed. They are created using pointers and memory management functions such as new and delete. In this article, we will learn different ways to initialize a dynamic arrays in C++. Here is the list of approaches to initialize a dynamic array in C++: Using new and () Using new and {} Using Loop for Custom Initialization Initializing Dynamic Arrays Using new and () This ... Read More

How to use std::sort to sort an array in C++

Farhan Muhamed
Updated on 29-May-2025 19:13:01

9K+ Views

Sorting of an array refer to the process of arranging the elements of the array in ascending or descending order. In this article, we will learn how to use the std::sort function from the C++ STL library to sort an array. In this problem, you are given an array of integers, and you need to arrange the elements in ascending order using std::sort. For example: // Input array int arr[] = {5, 2, 9, 1, 5, 6}; // Output {1, 2, 5, 5, 6, 9} // Explanation: The array is sorted in ascending order. C++ ... Read More

How to pass an array by reference in C++

Farhan Muhamed
Updated on 29-May-2025 19:13:47

7K+ Views

Passing an array by reference is a method of passing arrays to functions in C++ that allows the function to access and modify the original array without creating a copy. In this article, we will learn how to pass arrays by reference, the benefits of this approach, and provide examples. Passing an Array by Reference In the pass by reference approach, the address location of the first element of the array is passed to user defined functions. So, technically, we are passing the same array to that function without creating a copy. The changes made to the array ... Read More

Find size of array in C/C++ without using sizeof

Jennifer Nicholas
Updated on 03-Mar-2025 13:16:03

6K+ Views

In C/C++, we usually use the sizeof operator to find the size of an array. However, once an array is passed to a function, its size is lost. So, how can we find the size of an array without using sizeof? For example, consider this array: int arr[] = {1, 2, 3, 4, 5}; Without using sizeof, we can use a loop to count the number of elements in the array. In this article, we'll show you different methods to find the size of an array in C or C++ without using sizeof, with clear and simple examples. ... Read More

How to create a dynamic 2D array inside a class in C++

Farhan Muhamed
Updated on 28-May-2025 17:37:28

870 Views

A dynamic 2D array is an 2D array in which the size of array is determined during runtime and stored using pointers. In this article, we will learn how to create a dynamic 2D array inside a class in C++. First of all, let's understand the problem statement. In this problem, you need to create a class that contains a dynamic 2D array as its data member. The memory allocation and deallocation must be handled using constructor and destructor. For example: // Create a class with dynamic 2D array ClassMatrix m(3, 4); // Output Matrix of size ... Read More

Convert string to char array in C++

Nishu Kumari
Updated on 28-May-2025 18:05:05

4K+ Views

A string in C++ is a group of characters written inside double quotes like "Hello" and characters are the single letters or symbols in a string. Converting a string to a char array means putting each character into a simple list (array) of characters. In this article, we will show you how to convert a string to a character array in C++. For example, if we have the string "Hello@TP", converting it to a char array means storing each character 'H', 'e', 'l', 'l', 'o', '@', 'T', and 'P' separately in an array.How to Convert a String into a Character ... Read More

Advertisements