Table of Contents
Pointer
Example
Array
Output
Home Backend Development C++ The difference between pointers and arrays in C language

The difference between pointers and arrays in C language

Aug 26, 2023 pm 08:49 PM
Keyword extraction: difference array pointer

The difference between pointers and arrays in C language

The details about pointers and arrays showing their differences are as follows.

Pointer

A pointer is a variable that stores the address of another variable. When memory is allocated to a variable, the pointer points to the memory address of the variable. The unary operator ( * ) is used to declare pointer variables.

The following is the syntax for pointer declaration.

datatype *variable_name;
Copy after login

Here, datatype is the data type of the variable, such as int, char, float, etc., and variable_name is the variable name given by the user.

The following is a program that demonstrates pointers.

Example

Online demonstration

#include <stdio.h>
int main () {
   int a = 8;
   int *ptr;
   ptr = &a;
   printf("Value of variable a: %d</p><p>", a);
   printf("Address of variable a: %d</p><p>", ptr);
   return 0;
}
Copy after login

The output of the above program is as follows.

Value of variable a: 8
Address of variable a: -2018153420
Copy after login

Array

An array is a collection of elements of the same type located in contiguous memory locations. The lowest address in the array corresponds to the first element, while the highest address corresponds to the last element. Array indexing starts at zero (0) and ends with the array size minus one (array size - 1).

Output

The following is the syntax of the array.

Output

The following is the syntax of the array. >

type array_name[array_size ];
Copy after login

Here, array_name is the name of the array, and array_size is the size of the array.

The program to demonstrate the array is as follows.

Example

Live demonstration

#include <stdio.h>
int main () {
   int a[5];
   int i,j;
   for (i = 0;i<5;i++) {
      a[i] = i+100;
   }
   for (j = 0;j<5;j++) {
      printf("Element[%d] = %d</p><p>", j, a[j] );
   }
   return 0;
}
Copy after login

Output

The output results of the above program are as follows.

Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Copy after login

The above is the detailed content of The difference between pointers and arrays in C language. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The difference between WML and HTML The difference between WML and HTML Aug 25, 2023 pm 12:21 PM

Markup languages ​​like HTML and WML are mainly used to deliver website content. The system that each language targets is the basic difference between WML and HTML. HTML was developed for delivering material to personal computers that have sufficient computing power to process and render the information. When the Internet began to spread to mobile devices, it quickly became clear that these devices lacked the processing power, screen size, and color gamut range to support HTML. WML was created as a replacement for HTML for serving web content to mobile devices. Wireless Markup Language (WML) has been developed to specify the structure and content of data presentation on low-bandwidth devices such as cell phones and pagers. WML is based on XML markup language and is essentially a handheld

Challenges and solutions for PHP Hyperf microservice development Challenges and solutions for PHP Hyperf microservice development Sep 12, 2023 am 08:25 AM

Challenges and Solutions of PHPHyperf Microservice Development Introduction: With the rapid development of the Internet and mobile Internet, microservice architecture has gradually become a hot spot for developers. As one of the most popular development languages, PHP has also ushered in the era of microservice architecture. In the field of PHP, the PHPHyperf framework can be said to be one of the most popular microservice frameworks currently. However, microservice development still faces some challenges in practice. This article will explore these challenges and propose corresponding solutions. 1. Challenge One: Distribution

Specific steps to install win7 system on computer with one click Specific steps to install win7 system on computer with one click Jul 20, 2023 pm 05:21 PM

There are many ways to install the win7 system. Many people will install the win7 system through the hard disk. The editor of this article will specifically introduce you to the one-click installation method, which is also the simplest method. Don’t miss it if you don’t know how. 1. First, we open the computer browser and search the official website of Magic Pig One-Click System Reinstallation, download it and open it. 2. After downloading, we open it and click online reinstallation. 3. Next, we will wait patiently for it to be installed. 4. The installation is complete. Next we need to click to restart the computer now. 5. After restarting the computer, we still need to return to the main interface to continue completing the installation. Then our installation is completed. After completing the above operations, we can complete the one-click installation of the win7 system. I hope it will be helpful to everyone.

The difference between packages and interfaces in Java The difference between packages and interfaces in Java Sep 07, 2023 pm 10:17 PM

In this article, we will understand the difference between packages and interfaces in Java. Package It is a group of classes and/or interfaces that are grouped together. It can be created using the "Package" keyword. Can be imported. This can be done using the "import" keyword. Example packagepackage_name;publicclassclass_name{ . (bodyofclass) .} Interface It is a set of abstract methods and constants. Can be created using the "Interface" keyword. Can be picked up by another

How to use the selenium module for automated testing of web pages in Python 3.x How to use the selenium module for automated testing of web pages in Python 3.x Jul 30, 2023 pm 08:45 PM

How to use the selenium module in Python3. This article will introduce how to use Python3 and selenium modules for automated testing of web pages, and provide readers with some code examples. 1. Install selenium module

The difference between pointers and arrays in C language The difference between pointers and arrays in C language Aug 26, 2023 pm 08:49 PM

The details about pointers and arrays showing their differences are as follows. Pointer A pointer is a variable that stores the address of another variable. When memory is allocated to a variable, the pointer points to the memory address of the variable. The unary operator (*) is used to declare pointer variables. Following is the syntax for pointer declaration. datatype*variable_name; Here, datatype is the data type of the variable, such as int, char, float, etc., and variable_name is the variable name given by the user. A program demonstrating pointers is given below. Example Online Demonstration #include<stdio.h>intmain(){&

Learn the regular expression function in Go language and implement email format verification Learn the regular expression function in Go language and implement email format verification Jul 30, 2023 am 09:16 AM

Learn the regular expression function in Go language and implement mailbox format validation. Regular expression is a powerful tool for matching and processing text strings. In the Go language, text matching and processing can be achieved through regular expression functions, including email format verification. In this article, we will learn how to use the regular expression function in the Go language and implement verification of the email format through an example. Importing the regular expression package Before starting, we first need to import the regular expression package in the Go language. In Go language

Introduction to Python functions: usage and examples of divmod function Introduction to Python functions: usage and examples of divmod function Nov 04, 2023 am 10:57 AM

Introduction to Python functions: Usage and examples of the divmod function In Python, the divmod() function is used to find the integer quotient and remainder of two numbers. This function takes two arguments, the dividend and the divisor, and returns a tuple containing the integer quotient and remainder. The result returned by divmod(x,y) is a tuple containing two elements. The first element is the integer quotient obtained by dividing x by y, and the second element is the remainder obtained by dividing x by y. If x and y are both integers,

See all articles