YouTip LogoYouTip

C Pointer To An Array

body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; line-height: 1.6; color: #333; max-width: 900px; margin: 0 auto; padding: 20px; } h1, h2, h3 { color: #2c3e50; } a { color: #3498db; text-decoration: none; } a:hover { text-decoration: underline; } pre { background-color: #f8f8f8; border: 1px solid #ddd; border-radius: 4px; padding: 12px; overflow-x: auto; font-family: "Courier New", Courier, monospace; font-size: 14px; line-height: 1.5; } code { font-family: "Courier New", Courier, monospace; background-color: #f8f8f8; padding: 2px 4px; border-radius: 3px; } .note { background-color: #f0f7ff; border-left: 4px solid #3498db; padding: 12px; margin: 20px 0; } .warning { background-color: #fff8e1; border-left: 4px solid #ffc107; padding: 12px; margin: 20px 0; } .output { background-color: #f5f5f5; border: 1px solid #ddd; padding: 12px; border-radius: 4px; font-family: "Courier New", Courier, monospace; white-space: pre-wrap; } .comment { color: #6a9955; }

C Pointer to an Array |

-- Learning not just technology, but dreams!

C Tutorial

C Language Tutorial C Introduction C Environment Setup C VScode C Program Structure C Basic Syntax C Data Types C Variables C Constants C Storage Classes C Operators C Decision C Loops C Functions C Scope Rules C Arrays C enum(Enumeration) C Pointers C Function Pointers and Callback Functions C Strings C Structures C Unions C Bit Fields C typedef C Input & Output C File I/O C Preprocessors C Header Files C Type Casting C Error Handling C Recursion C Variable Arguments C Memory Management C Undefined Behavior C Command Line Arguments C Safe Functions C Sorting Algorithms C Project Structure C Examples C Classic 100 Examples C Quiz

C Standard Library

C Standard Library - Reference Manual <a href="#" title="C Standard Library - ">C Standard Library - <assert.h> <a href="#" title="C Standard Library - ">C Standard Library - <ctype.h> <a href="#" title="C Standard Library - ">C Standard Library - <errno.h> <a href="#" title="C Standard Library - ">C Standard Library - <float.h> <a href="#" title="C Standard Library - ">C Standard Library - <limits.h> <a href="#" title="C Standard Library - ">C Standard Library - <locale.h> <a href="#" title="C Standard Library - ">C Standard Library - <math.h> <a href="#" title="C Standard Library - ">C Standard Library - <setjmp.h> <a href="#" title="C Standard Library - ">C Standard Library - <signal.h> <a href="#" title="C Standard Library - ">C Standard Library - <stdarg.h> <a href="#" title="C Standard Library - ">C Standard Library - <stddef.h> <a href="#" title="C Standard Library - ">C Standard Library - <stdio.h> <a href="#" title="C Standard Library - ">C Standard Library - <stdlib.h> <a href="#" title="C Standard Library - ">C Standard Library - <string.h> <a href="#" title="C Standard Library -

C Scope Rules

C enum(Enumeration)

C Pointer to an Array

C Arrays C Arrays

You can skip this chapter first and come back to learn its content after you understand the concept of C pointers.

If you have some understanding of the concept of pointers in C language, then you can start learning this chapter.

The array name itself is a constant pointer, meaning its value cannot be changed. Once determined, it cannot point elsewhere.

Therefore, in the following declaration:

double balance;

balance is a pointer to &balance, which is the address of the first element of the array balance. Therefore, the following code snippet assigns p the address of the first element of balance:

double *p;
double balance;
p = balance;

Using the array name as a constant pointer is legal, and vice versa. Therefore, *(balance + 4) is a valid way to access the data in balance.

Once you store the address of the first element in p, you can use *p, *(p+1), *(p+2), etc., to access the array elements. The following example demonstrates these concepts discussed above:

Example

#include<stdio.h>

int main()
{
   /* An integer array with 5 elements */
   double balance = {1000.0, 2.0, 3.4, 17.0, 50.0};
   double *p;
   int i;

   p = balance;

   /* Output each element's value */
   printf("Array values using pointern");
   for ( i = 0; i < 5; i++ )
   {
       printf("*(p + %d) : %fn", i, *(p + i));
   }

   printf("Array values using balance as addressn");
   for ( i = 0; i < 5; i++ )
   {
       printf("*(balance + %d) : %fn", i, *(balance + i));
   }

   return 0;
}

When the above code is compiled and executed, it produces the following result:

Array values using pointer *(p + 0) : 1000.000000 *(p + 1) : 2.000000 *(p + 2) : 3.400000 *(p + 3) : 17.000000 *(p + 4) : 50.000000 Array values using balance as address *(balance + 0) : 1000.000000 *(balance + 1) : 2.000000 *(balance + 2) : 3.400000 *(balance + 3) : 17.000000 *(balance + 4) : 50.000000

In the above example, p is a pointer to a double, which means it can store the address of a double variable. Once we have the address in p, *p will give the value stored at the corresponding address in p, as demonstrated in the example above.

C Arrays C Arrays

2 Notes

#0 tuingke

142***2460@qq.com

About the double type and float type:

printf() only sees double-precision numbers. The %f format specifier for printf always yields a double. Therefore, using %f or %lf in printf() produces the same output display. However, for variables, the double type has higher precision than the float type. "Higher precision" for double means it stores more decimal places, but the default output is 6 decimal places. If you want to output more decimal places, you can control it yourself, for example, %.10lf will output 10 decimal places.

So generally, the placeholder for a double type can be %lf.

More content can be found at: Difference between float and double types

tuingke 8 years ago (2018-07-14)

#0 beginner

bry***qiqi@gmail.com

A pointer to the first element of an array is equivalent to a pointer to the array:

#include <stdio.h>

int main ()
{
   /* An integer array with 5 elements */
   double balance = {1000.0, 2.0, 3.4, 17.0, 50.0};
   double *p;
   int i;

   p = balance;

   /* Output each element's value */
   printf( "Array values using pointern");
   printf( "Array values using p as addressn");
   for ( i = 0; i < 5; i++ )
   {
       printf("p[%d]: %fn", i, p );
   }

   return 0;
}

beginner 6 years ago (2020-09-08)

Click here to share notes

Write notes...

Image URL

Image description

Image size Γ— Restore image size

Share notes

  • Nickname (Required)
  • Email (Required)
  • Reference URL

Category Navigation

← Event OnfocusoutC Passing Arrays To Functions β†’