40 C Arrays
In the last chapter, you worked with C strings. A C string turned out to be a list of characters packed one next to the other in memory. C arrays are lists of other data types packed one next to the other in memory. Just as with strings, you deal with the list by holding onto the address of the first one.
Imagine that you wanted to write a program that would calculate the average of 3 grades. Create a new C Command Line Tool project and name it gradeInTheShade.
Edit main.c:
#include <stdio.h> #include <stdlib.h> // malloc(), free() float averageFloats(float *data, int dataCount) { float sum = 0.0; for (int i = 0; i < dataCount; i++) { sum = sum + data[i]; } return sum / dataCount; } int main (int argc, const char * ...
Get Objective-C Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.