Declaring%20Arrays - PowerPoint PPT Presentation

About This Presentation
Title:

Declaring%20Arrays

Description:

A string is an array of characters (C has no String type) The entire array may not be filled ... Compile: gcc g main.c func.c o main. Invoke: gdb main ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 13
Provided by: set134
Learn more at: http://cs.sou.edu
Category:

less

Transcript and Presenter's Notes

Title: Declaring%20Arrays


1
Declaring Arrays
  • Declare an array of 10 elements int nums10
  • Best practicedefine SIZE 10 int
    numsSIZE // cannot be intSIZE nums
  • C99 int numssomeVariable
  • Declare an array with an initializer listint
    nums 1, 2, 3, 4, 5, 6, 7, 8, 9, 10Note
    Don't need a number between and
  • Access elements like primitive variablesprintf
    ("d\n", numsi)
  • Declare in a function void foo(int nums) or
    void foo(int nums)

2
Pointer arithmetic
2
x
Address
  • int i, nums6
  • for (i0 ilt6 i)
  • numsi i10
  • int np nums
  • (np3) 33
  • nums5 66
  • (np2) 22
  • int x (np2) - (np0)

1000
1004
1008
1012
1016
1020
Nums
Note (nums 1) is not equal to nums 1
3
Accessing Arrays using pointers
An array is just a pointer to a contiguous block
of memory
  • Declaring in a functionvoid foo(char data)
  • Accessing the 10th elementprintf("c\n", (data
    9))
  • Another waychar ptr data9printf("c\n",
    ptr)
  • Declare int x
  • Be careful, there is no memory allocated
  • C does no memory bound checks

4
Notes on Pointers
  • C Programmers often favor pointers to process
    arrays (less typing)
  • You can use all the relational operators with
    pointers (lt, gt, , 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 i0, sum0
  • for (i0 ilts i)
  • sum numsi
  • printf("d\n", sum)
  • Call
  • int nums 1, 2, 3
  • foo(nums, 3)
  • Declaration
  • void foo(const int nums
  • , int s)
  • int sum0, ptrfor(ptrnums ptr-numslts
    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 data6 data0 'a'
    data1 'b' data2 'c' data3 '\0'
  • Declaring a string using a literal char data
    "abc"
  • Replace the third character data2 'd' or
    (data2) 'd'
  • Note 'a' is not "a". Question How do they
    differ?

7
Inputting Strings (scanf with s)
  • Note scanf reads till it sees white space
  • Example char s2 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)

8
String Output
  • Print entire string printf("s\n", str)
  • Character by character (a line each)int ifor
    (i0 stri!'\0' i) printf("c\n", stri)
  • Another wayint i, len strlen(str)for (int
    i0 iltlen i) printf("c\n", stri)

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 anotherchar strcpy(char
    toStr, const char fromStr)
  • Compare Strings like the Java Comparable
    interface int strcmp(const char s1, const char
    s2)
  • Concatenate a stringchar 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 ltstdio.hgt
  • 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
    UNIQUENAMEdefine UNIQUENAMEendif
  • file1.hdefine MAX 5
  • file2.h
  • include "file1.h"
  • prog.cinclude file1.hinclude 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 ltline gt or break ltfunction
    namegt
  • List break points break or list watches display
  • List source list ltline gt or list ltfunction
    namegt
  • Step into step or Step over next or Continue
    cont
  • Display variables and expressions print
    ltexpressiongt
  • Create a watch display ltexpressiongt
  • 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)
Write a Comment
User Comments (0)
About PowerShow.com