", i+1 ); scanf( "%d", &test_scores[i] ); sum = sum + test_scores[i]; ave = sum/10; } printf("\nThe average of the ten scores is %f", ave); printf( "\nThank you.\n" ); return 0; } week score.c"> ", i+1 ); scanf( "%d", &test_scores[i] ); sum = sum + test_scores[i]; ave = sum/10; } printf("\nThe average of the ten scores is %f", ave); printf( "\nThank you.\n" ); return 0; } week score.c">
Download presentation
Presentation is loading. Please wait.
1
C Arrays
2
One-Dimensional Arrays Proper ways of declare an array in C: int hours[ 24]; double Temp [24]; int test_score [15] = { 77, 88, 99, 100, 87, 78, 66, 55, 44, 98}; char Mid_Initial [15] = { ‘A’, ‘B’, ‘C’}; int number[10] = { 1, 2, 3, 4};
3
Example #include int main( void ) { int test_scores[10]; int i; double sum = 0; double ave = 0; printf( "Please enter the ten test scores now.\n" ); for ( i = 0; i < 10; i++ ) { printf( "#%2d > ", i+1 ); scanf( "%d", &test_scores[i] ); sum = sum + test_scores[i]; ave = sum/10; } printf("\nThe average of the ten scores is %f", ave); printf( "\nThank you.\n" ); return 0; } week06 ----score.c
4
sizeof arrays int main( void ) { char chararray[NUMCHARS]; int intarray[] = { 2, 1, 3, 5, 4, 8, 3, 7 }; double dblarray[NUMFLOATS] = { 1.2, 3.4, -2.3, 1.4, 4.5 }; int index, i; int another[10] = {0, 1, 2, 3}; for ( i = 0; i < 10; i++) printf("The elements in array another are: %d\n", another[i]); for ( index = 0; index < NUMCHARS; index++ ) chararray[index] = 127 - index; printf( "chararray occupies %d bytes.\n", sizeof( chararray ) ); printf( "intarray occupies %d bytes.\n", sizeof( intarray ) ); printf( "dblarray occupies %d bytes.\n", sizeof( dblarray ) ); printf( "The element in chararray with index 3 is '%c'.\n", chararray[3] ); printf( "The element in intarray with index 3 is %d.\n", intarray[3] ); printf( "The element in dblarray with index 3 is %5.2f.\n", dblarray[3] ); return 0; } week06 ----array1.c
5
Character Arrays ***We almost always terminate a character array with a null character ‘\0’ for a character array so that it can be treated as a string. Char name[5] = { ‘A’, ‘d’, ‘a’, ‘m’, ‘/0’};
6
Declare an array without size ***Example: int first_case[] = {1, 2, 3, 4, 5, 6, 7); Double second_case ( int num[]); Only these two situations [ ] are allowed. Therefore, it is important to put the size of an array in most situations.
7
Example arrayptrA.c ---- week06
9
Array and Pointers
16
Why do we use pointers? 1. If we want to pass a huge struct or array, it is easier to pass a pointer than the whole thing. 2. In general, pointers allow cleaner, more compact code.
17
Drawbacks? Pointers probably the single largest source of bugs in software, so be careful anytime you deal with them! Dangling pointers Memory leaks
18
Example void f() { int *ptr; *ptr = 55; } What is wrong with this code?
19
Example #include int main( void ) { int intvar = 65535; printf( "The values of intvar are stored in the memory\n" ); printf( "The value of intvar is %d\n", intvar); printf( "The value of intvar is %x\n", intvar); printf( "The address of intvar is %p\n", intvar); /* ??? */ printf( " The address of intvar is %p.\n", &intvar ); printf( "The address of intvar is %x\n", &intvar); printf( "How about this ? %d\n", &intvar); return 0; } week06 --- address1A.c
20
Example: #include int main( void ) { int intvar, *int_ptr; char charvar, *char_ptr = &charvar; int_ptr = &intvar; printf( "The address of charvar is %p.\n", char_ptr ); printf( "The next character could be stored at %p.\n", char_ptr + 1 ); printf( "The address of intvar is %p.\n", int_ptr ); printf( "The next integer could be stored at %p.\n", int_ptr + 1 ); return 0; } week06 --- address3.c
21
Example #include int main( void ) { int intvar = 65535, *int_ptr; char charvar, *char_ptr = &charvar; int_ptr = &intvar; printf( "The address of charvar is %p.\n", char_ptr ); printf( "The next character could be stored at %p.\n", char_ptr + 1 ); printf( "The value of intvar is %d\n", intvar); printf( "The current intvar is %d\n", *int_ptr); printf( "The address of intvar is %d.\n", int_ptr ); printf( "The next integer could be stored at %d.\n", int_ptr + 1 ); printf( "The next integer could be stored at %d.\n", int_ptr + 2 ); printf( "The address of intvar is %p.\n", int_ptr ); printf( "The next integer could be stored at %p.\n", int_ptr + 1 ); printf( "The next integer could be stored at %p.\n", int_ptr + 2 ); return 0; } week06 --- address3A.c
22
Pointers and Parameter Passing
25
What is a Bus Error? A bus error is generally an attempt to access memory that the CPU cannot physically address. Bus errors can also be caused by any general device fault that the computer detects. A bus error rarely means that computer hardware is physically broken. It is normally caused by buggy software trying to access something it can't.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.