The Difference Between Pointers and Arrays

The Difference Between Pointers and Arrays

Some programmers believe that int a[] and int * a are equivalent.Also some C++ books to say that pointers and arrays are almost same. This belief usually leads to hard to find bugs in the program. Suppose we have a function:

 void f(int *p);

We can call it either passing a variable of type int * or int[]. For example:

 	int * p;	int a[10];	...	//initialize a and p here..	...	...	//we can call f by either passing p or a	f(a);	f(p);

The code generated by the compiler when accessing the pointers and array variables is different. When we say int * p, there exists a physical memory that points to an integer. But when we say int a[], there is no such location, and it is the compiler that generates the proper addresses wherever it is referenced.

Suppose you have a declared an array in a source file and want to use the array from another source file. The common practice is to declare the array as extern in the file where it is being used. The following program will crash:

 //test1.cppextern int a[5] = {1,2,3,4,5};...//test2.cppextern int * a;main(){   a[0] = 10;}

It crashes because in test2.cpp, the extern declaration is not consistent with that of in test1.cpp. The correct extern declaration in test2.cpp should be as below:

 extern int a[];

Most compilers won

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes