Presentation is loading. Please wait.

Presentation is loading. Please wait.

Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable]

Similar presentations


Presentation on theme: "Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable]"— Presentation transcript:

1 Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable] Declare an array with an initializer list int nums[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Note: Don't need a number between [ and ] Access elements like primitive variables printf ("%d\n", nums[i]); Declare in a function: void foo(int nums[]) or void foo(int *nums)

2 Pointer arithmetic int i, nums[6]; for (i=0; i<6; i++) nums[i] = i*10; int *np = nums; *(np+3) = 33; nums[5] = 66; *(&np[2]) = 22; int x = &(np[2]) - &(np[0]) 1000 1004 1008 1012 1016 1020 Address 10 66 50 33 22 20 Content 2 x Nums Note: *(nums + 1) is not equal to *nums + 1

3 Accessing Arrays using pointers Declaring in a function: void foo(char *data) Accessing the 10 th element printf("%c\n", *(data + 9)); Another way char *ptr = data+9; printf("%c\n", *ptr); Declare: int *x; –Be careful, there is no memory allocated –C does no memory bound checks An array is just a pointer to a contiguous block of memory

4 Notes on Pointers C Programmers often favor pointers to process arrays (less typing) You can use all the relational operators with pointers (, ==, etc.) You can use all the arithmetic operations (++, +=, --, etc.) The size in bytes of pointers can be obtained using the sizeof operator

5 Examples with functions Declaration void foo (const int nums[], int s) { int i=0, sum=0; for (i=0; i<s; i++) sum += nums[i]; printf("%d\n", sum); } Call int nums[] = {1, 2, 3}; foo(nums, 3); Declaration void foo(const int *nums, int s) { int sum=0, *ptr; for(ptr=nums; ptr-nums<s; ptr++) sum += *ptr; printf("%d\n", sum); } Call int nums[] = {1, 2, 3}; foo(nums, 3); Note: The const modifier is not required, some arrays can be changed

6 Strings In C –A string is an array of characters (C has no String type) –The entire array may not be filled –Unlike Java, strings are mutable –The string is terminated by a null ('\0') character The hard way: char data[6]; data[0] = 'a'; data[1]= 'b'; data[2] = 'c'; data[3] = '\0'; Declaring a string using a literal: char[] data = "abc"; Replace the third character: data[2] = 'd'; or *(data+2) = 'd'; Note: 'a' is not "a". Question: How do they differ? 'a''b''c''\0'???

7 Inputting Strings (scanf with %s) Note: scanf reads till it sees white space Example:char s[2]; scanf("%s", s); Problem: inputting "ab" stores '\0' outside the bounds of the array Result: possible "Segmentation Fault" Solution: Be sure to define enough space Another Solution: Use fgets (later topic) 'a''b' '\0'

8 String Output Print entire string: printf("%s\n", str); Character by character (a line each) int i; for (i=0; str[i]!='\0'; i++) printf("%c\n", str[i]); Another way int i, len = strlen(str); for (int i=0; i<len; i++) printf("%c\n", str[i]);

9 String Functions Header file: string.h Length of a string (excluding the null ('\0')) int strlen: int strlen(const s[]) Copy from one string to another char* strcpy(char toStr[], const char fromStr[]) Compare Strings: like the Java Comparable interface int strcmp(const char s1[], const char s2[]) Concatenate a string char* strcat(char toStr[], const char fromStr[]) Notes: –Make sure that the destination string is big enough –There are many more string functions than these –Some (not all) systems include string functions in stdio.h –If there is no null ('\0') bizarre things can happen

10 Header Files Look usual places: #include Look in local folder: #include "header.h" Put in your header files: –includes: #include –constants: #define –other preprocessing directives: #ifndef (see next slide)

11 #ifndef Problem: If more than one header refers to stdio, it will be included twice Solution: use "if not defined directive" #ifndef UNIQUENAME #define UNIQUENAME #endif file1.h #define MAX 5 file2.h #include "file1.h" prog.c #include file1.h #include file2.h MAX is defined twice

12 GDB Debugger Compile: gcc –g main.c func.c –o main Invoke: gdb main Popular commands: –break point: break or break –List break points: break or list watches: display –List source: list or list –Step into: step or Step over: next or Continue: cont –Display variables and expressions: print –Create a watch: display –Run the program: run or Exit debugger: quit –Delete watch: delete display # or Delete breakpoint delete # –Current stack record: where and Caller's stack record: up –GDB Help: help or help command Note: There are cheat sheets available that can help (see class web site)


Download ppt "Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable]"

Similar presentations


Ads by Google