0% found this document useful (0 votes)
32 views11 pages

C 29

Uploaded by

hlemorvan
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
0% found this document useful (0 votes)
32 views11 pages

C 29

Uploaded by

hlemorvan
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
You are on page 1/ 11
‘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 am Pointers 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