Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
32 views
11 pages
C 29
Uploaded by
hlemorvan
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save c29 For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
32 views
11 pages
C 29
Uploaded by
hlemorvan
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save c29 For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save c29 For Later
You are on page 1
/ 11
Search
Fullscreen
‘1rr212028 20.48 Pointers and Arcays 3 w Tutorials» —Exercisesw Servicese§ QO Log in schools C Pointers and Arrays [errs] Loew | Pointers & Arrays You can also use pointers to access arrays. Consider the following array of integers: Example int myNumbers[4] = {25, 58, 75, 100}5 You learned from the arrays chapter that you can loop through the array elements with a for loop: Example int myNumbers[4] = {25, 58, 75, 100}; int i; for (i = 0; i < 45 itt) ( print#("%d\n", myNumbers[i]) 5 d Result: nitpsdlmwwsvdschools.comice_pointers_arrays.ahp un‘1rr212028 20.48 Pointers and Arcays 3 w Tutorials» —Exercisesw Servicese§ QO Log in schools Instead of printing the value of each array element, let's print the memory address of each array element: Example int myNumbers[4] = {25, 58, 75, 100}; int i; for (i print#(" + i <4; ist) { p\n", &myNumbers[i]); Result: Note that the last number of each of the elements' memory address is different, with an addition of 4. It is because the size of an int type is typically 4 bytes, remember: Example // Create an int variable int myInt; // Get the memory size of an int nitpsilmww:vdschools.comile_pointers_arrays.ahp amPointers and Arcays Tutorials Exercisesw Servicesey§ QO O Login So from the "memory address example" above, you can see that the compiler reserves 4 bytes of memory for each array element, which means that the entire array takes up 16 bytes (4 * 4) of memory storage: Example int myNumbers[4] = {25, 5, 75, 100}; // Get the size of the myNumbers array printf("%1u", sizeof(myNumbers)) ; Result: How Are Pointers Related to Arrays Ok, so what's the relationship between pointers and arrays? Well, in C, the name of an array, is actually a pointer to the first element of the array. Confused? Let's try to understand this better, and use our "memory address example" above again. The memory address of the first element is the same as the name of the array Example nitpsdlmwwsvdschools.comice_pointers_arrays.ahp aint‘1rr212028 20.48 Pointers and Arcays 3 w Tutorials» —Exercisesw Servicese§ QO Log in schools // Get the memory address of the first array element printf("%p\n", &myNumbers[@]); Result: This basically means that we can work with arrays through pointers! How? Since myNumbers is a pointer to the first element in myNumbers, you can use the * operator to access it: Example int myNumbers[4] = {25, 58, 75, 100}; // Get the value of the first element in myNumbers printf("%d", *myNumbers) ; Result: To access the rest of the elements in myNumbers, you can increment the pointer/array (+1, +2, etc) Example nitpsdlmwwsvdschools.comice_pointers_arrays.ahp am‘1rr212028 20.48 Pointers and Arcays 3 WW tutoriaisy Exercises» Servicesse§ Q 0 schools // Get the value of the third element in myNumbers printf("%d", *(myNumbers + 2)); // and so on.. Result: Or loop through it: Example int myNumbers[4] = {25, 50, 75, 100}; int *ptr = myNumbers; int i; for (i = 0; i < 45 itt) { printf("%d\n", *(ptr + i))5 + Result: It is also possible to change the value of array elements with pointers: nitpsdlmwwsvdschools.comice_pointers_arrays.ahp Log in sn‘1rr212028 20.48 Pointers and Arcays 3 w Tutorials» —Exercisesw Servicese§ QO Log in schools ANG myNUMUEE>L#J = 1225 209 125 40Osy // Change the value of the first element to 13 *myNumbers = 13; // Change the value of the second element to 17 *(myNumbers +1) = 175 // Get the value of the first element printf("%d\n", *myNumbers) ; // Get the value of the second element printf("%d\n", *(myNumbers + 1)); Result: This way of working with arrays might seem a bit excessive. Especially with simple arrays like in the examples above. However, for large arrays, it can be much more efficient to access and manipulate arrays with pointers. It is also considered faster and easier to access two-dimensional arrays with pointers. And since strings are actually arrays, you can also use pointers to access strings. For now, it's great that you know how this works. But like we specified in the previous chapter; pointers must be handled with care, since it is possible to overwrite other data stored in memory. ADVERTISEMENT nitpsdlmwwsvdschools.comice_pointers_arrays.ahp ent‘1rr212028 20.48 Pointers and Arcays 3 w Tutorials~ Exercisese Serviesey§ QO SignUp Login schools = CSS JAVASCR SQL. PYTHON JAVA PHP, = HOWTO W3.CSS_ hntipsifinww.w3schoots.comicle_peinters_arrays.php mH‘1rr212028 20.48 Pointers and Arcays Tutorials~ Exercisese Serviesey§ QO SignUp Login schools SQL. PYTHON JAVA PHP. = HOWTO _—_—W3,CSS Certificate Today And Unlock Tomorrow's Opportunities! Cele Tale Don't let hesitation hold you back. a The besttime to start snow. COLOR PICKER CSS JAVASCRIPT ADVERTISEMENT hntipsifinww.w3schoots.comicle_peinters_arrays.php ant‘nir2r2023 20.48 Pointers and Arcays 3 w Tutorials» —Exercisesy Servicesey§ QO O SignUp Login = CSS JAVASCRIPT SQL. PYTHON. «JAVA. PHP-«S HOWTO W3,CSS_—C ‘ADVERTISEMENT ee » hntipsifinww.w3schoots.comicle_peinters_arrays.php ont‘nir2r2023 20.48 Pointers and Arcays 3 w Tutorialsy Exercises» Servicesy§ Q @ SignUp Login = css JAVASCRIPT SQL PYTHON JAVA PHP. HOW TO. W3.CSS Cc vy oeaces orsnave Newsue rex schools GET CERTIFIED REPORT ERROR Top Tutorials HTML Tutorial 5S Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial Top References HTML Reference CSS Reference JavaScript Reference SQL Reference Python Reference WA.CSS Reference Bootstrap Reference PHP Reference HTML Colors Java Reference ‘Angular Reference Query Reference Top Examples Get Certified HTML Examples HTML certificate 5s Examples CSS Certificate JavaScript Examples JavaScript Certificate How To Examples Front End Certificate SQL Examples SQU Certificate Python Examples Python certificate W2.CSS Examples PHP Certificate Bootstrap Examples iguery Certificate PHP Examples ‘ava Certificate Java Examples c+ certificate XML Beamples Ch ceriicate jQuery Examples XL Certificate G@ @ ® © Forum asour hntipsifinww.w3schoots.comicle_peinters_arrays.php sont‘1rr212028 20.48 Pointers and Arcays 3 w Tutorials~ Exercisese Serviesey§ QO SignUp Login schools = CSS JAVASCRIPT SQL. PYTHON = JAVA. PHP-—«S HOWTO W3.CSS_— CC Copyright 1999-2023 by Refsni W3.cSs. Data. All Rights Reserved, W3Schools Is Powered by hntipsifinww.w3schoots.comicle_peinters_arrays.php wnt
You might also like
Unit 4
PDF
No ratings yet
Unit 4
30 pages
C 21
PDF
No ratings yet
C 21
9 pages
Ip 345 Units
PDF
No ratings yet
Ip 345 Units
21 pages
Lec 3
PDF
No ratings yet
Lec 3
30 pages
Adobe Scan Nov 15, 2024
PDF
No ratings yet
Adobe Scan Nov 15, 2024
13 pages
11 - Pointers-Arrays-Structures
PDF
No ratings yet
11 - Pointers-Arrays-Structures
22 pages
Pointers and Arrays
PDF
100% (1)
Pointers and Arrays
3 pages
Chapter 12
PDF
No ratings yet
Chapter 12
48 pages
Chapter 12: Pointers and Arrays
PDF
No ratings yet
Chapter 12: Pointers and Arrays
53 pages
C Arrays and Pointers: - in Java, Pointers Are Easy To Deal With
PDF
No ratings yet
C Arrays and Pointers: - in Java, Pointers Are Easy To Deal With
16 pages
Stractured Programming
PDF
No ratings yet
Stractured Programming
59 pages
What Is An Array in C
PDF
No ratings yet
What Is An Array in C
8 pages
Lect 18 Pointers Arrays Structures
PDF
No ratings yet
Lect 18 Pointers Arrays Structures
15 pages
3 Chapter 5 8 PDF Download of Teach Yourself C by Herbert Schildt
PDF
No ratings yet
3 Chapter 5 8 PDF Download of Teach Yourself C by Herbert Schildt
120 pages
ASD Course - Chap9 - Pointers and Array in C - Relationship and Use
PDF
No ratings yet
ASD Course - Chap9 - Pointers and Array in C - Relationship and Use
6 pages
Programming in C
PDF
No ratings yet
Programming in C
5 pages
Unit 3
PDF
No ratings yet
Unit 3
65 pages
Introduction To Arrays
PDF
No ratings yet
Introduction To Arrays
31 pages
Arrays in C Programming A Comprehensive Guide
PDF
No ratings yet
Arrays in C Programming A Comprehensive Guide
3 pages
Arrays (Incomplete)
PDF
No ratings yet
Arrays (Incomplete)
15 pages
Programming in C: Pointers and Arrays
PDF
No ratings yet
Programming in C: Pointers and Arrays
16 pages
Lecture4 Pointer&array
PDF
No ratings yet
Lecture4 Pointer&array
11 pages
Arrays in C Programming Language
PDF
No ratings yet
Arrays in C Programming Language
22 pages
DS Unit1 Part2
PDF
No ratings yet
DS Unit1 Part2
10 pages
Data Structure Lect 18 Pointers Arrays Structures
PDF
No ratings yet
Data Structure Lect 18 Pointers Arrays Structures
15 pages
Array Vs Pointer
PDF
No ratings yet
Array Vs Pointer
3 pages
C Arrays and Pointers: - in Java, Pointers Are Easy To Deal With
PDF
No ratings yet
C Arrays and Pointers: - in Java, Pointers Are Easy To Deal With
38 pages
10 Arrays
PDF
No ratings yet
10 Arrays
22 pages
Arrays in Data Structures and Algorithms DSA A
PDF
No ratings yet
Arrays in Data Structures and Algorithms DSA A
10 pages
Array of Pointers
PDF
No ratings yet
Array of Pointers
3 pages
Arrays
PDF
No ratings yet
Arrays
8 pages
CH 12
PDF
No ratings yet
CH 12
53 pages
Pointers PPT Cpps
PDF
No ratings yet
Pointers PPT Cpps
34 pages
Pointers and Arrays in C
PDF
No ratings yet
Pointers and Arrays in C
6 pages
C - Arrays
PDF
No ratings yet
C - Arrays
6 pages
Unit 3
PDF
No ratings yet
Unit 3
12 pages
Array & Pointer & String
PDF
No ratings yet
Array & Pointer & String
22 pages
DSA Unit-1 (2nd)
PDF
No ratings yet
DSA Unit-1 (2nd)
15 pages
L05 - Arrays
PDF
No ratings yet
L05 - Arrays
21 pages
Pointer Vs Array in C
PDF
No ratings yet
Pointer Vs Array in C
5 pages
Array in C
PDF
No ratings yet
Array in C
28 pages
Data Structure
PDF
No ratings yet
Data Structure
18 pages
Unit 5
PDF
No ratings yet
Unit 5
46 pages
Arrays in C
PDF
No ratings yet
Arrays in C
27 pages
ARRAYS and Pointers IN C - 1
PDF
No ratings yet
ARRAYS and Pointers IN C - 1
8 pages
Unit-4 LDP
PDF
No ratings yet
Unit-4 LDP
10 pages
Programming For Problem Solving Using C Unit-Iii: Arrays
PDF
No ratings yet
Programming For Problem Solving Using C Unit-Iii: Arrays
20 pages
Is A Ponter Variable, So When We Write PTRSL, Then Adding 1 Gives A Pointer That Points I Elements
PDF
No ratings yet
Is A Ponter Variable, So When We Write PTRSL, Then Adding 1 Gives A Pointer That Points I Elements
24 pages
NMCP Unit 03
PDF
No ratings yet
NMCP Unit 03
19 pages
05 Arrays and Strings (CS174)
PDF
No ratings yet
05 Arrays and Strings (CS174)
25 pages
C Unit 4
PDF
No ratings yet
C Unit 4
19 pages
Arrays in C
PDF
No ratings yet
Arrays in C
9 pages
ARRAYS in C Programming
PDF
No ratings yet
ARRAYS in C Programming
11 pages
Pointers (Part 1) : Computer Science and The Art of Programming, Harvard School of
PDF
No ratings yet
Pointers (Part 1) : Computer Science and The Art of Programming, Harvard School of
27 pages
CP UNIT III
PDF
No ratings yet
CP UNIT III
20 pages
Lec2 - Arrays (Part2)
PDF
No ratings yet
Lec2 - Arrays (Part2)
37 pages
Unit 4
PDF
No ratings yet
Unit 4
46 pages
Arrays and Strings
PDF
No ratings yet
Arrays and Strings
38 pages
Chapter 12 Pointers
PDF
No ratings yet
Chapter 12 Pointers
31 pages
C 52
PDF
No ratings yet
C 52
7 pages
C 55
PDF
No ratings yet
C 55
5 pages
C 44
PDF
No ratings yet
C 44
9 pages
C 50
PDF
No ratings yet
C 50
5 pages
C 30
PDF
No ratings yet
C 30
8 pages
C 45
PDF
No ratings yet
C 45
8 pages
C 41
PDF
No ratings yet
C 41
5 pages
C 26
PDF
No ratings yet
C 26
8 pages
C 33
PDF
No ratings yet
C 33
6 pages
C 36
PDF
No ratings yet
C 36
6 pages
C 43
PDF
No ratings yet
C 43
6 pages
C 22
PDF
No ratings yet
C 22
6 pages
C 32
PDF
No ratings yet
C 32
6 pages
C 31
PDF
No ratings yet
C 31
8 pages
C 39
PDF
No ratings yet
C 39
8 pages
C 11
PDF
No ratings yet
C 11
7 pages
C 5
PDF
No ratings yet
C 5
5 pages
C 27
PDF
No ratings yet
C 27
5 pages
C 17
PDF
No ratings yet
C 17
9 pages
C 25
PDF
No ratings yet
C 25
8 pages
C 12
PDF
No ratings yet
C 12
6 pages
C 20
PDF
No ratings yet
C 20
5 pages
C 3
PDF
No ratings yet
C 3
6 pages
C 14
PDF
No ratings yet
C 14
9 pages
C 9
PDF
No ratings yet
C 9
13 pages
C 1
PDF
No ratings yet
C 1
8 pages
C 6
PDF
No ratings yet
C 6
5 pages
C 2
PDF
No ratings yet
C 2
5 pages
C 8
PDF
No ratings yet
C 8
7 pages