1_pdfsam_cs2022_lectures - Example: typedef syntax As if...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Quiz Wednesday? Closed book Based on homeworks and programs gone through in class No need to memorize syntax If I want you to use a function/ technique, I will provide a small example
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Arrays of strings Arrays of strings are just 2-D arrays of characters Because strings are just arrays of characters char *array2[] = {“Test.”, “Vacation!”, “”}; array2 (char **) T E S T . \0 V a c a t i o n ! \0 \0 (char *) (char *) (char *)
Background image of page 2
Example #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int str_count, j; char **str_array, buf[1024]; printf(“How many strings do you have? ”); fgets(buf, 1024, stdin); str_count = atoi(buf); str_array = (char **) malloc(str_count * sizeof(char *)); for(j=0; j<str_count; j++) { fgets(buf, 1024, stdin); str_array[j] = (char *) malloc(strlen(buf) + 1); strcpy(str_array[j], buf); } for(j=0; j<str_count; j++) printf(“%s”, str_array[j]); for(j=0; j<str_count; j++) free(str_array[j]); free(str_array); return 0; }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Creating your own types C lets you name your own types using typedef
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Example: typedef syntax As if youre declaring a variable of the type you want to redefine Give the variable the name of the new type Put typedef in front // create a new type called scalar typedef double scalar; scalar add(scalar a, scalar b) { return a + b; } typedef example Use typedef to create alias for complex types // create new types called scalar and vector typedef double scalar; scalar add_scalars(scalar a, scalar b) { return a + b; } add_vectors(scalar result[10], scalar a[10], scalar b[10]) { int j; for(j=0; j<10; j++) result[j] = a[j] + b[j]; } typedef example Use typedef to create alias for complex types // create new types called scalar and vector typedef double scalar; typedef scalar vector[10]; scalar add_scalars(scalar a, scalar b) { return a + b; } add_vectors( vector result, vector a, vector b) { int j; for(j=0; j<10; j++) result[j] = a[j] + b[j]; }...
View Full Document

This note was uploaded on 01/10/2010 for the course CS 2022 at Cornell University (Engineering School).

Page1 / 6

1_pdfsam_cs2022_lectures - Example: typedef syntax As if...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online