Programmiersprache C/C++ Inhaltsverzeichnis: Programmieren in C. Programming in C. A. D. Marshall 1994-2005 Substantially Updated March 1999 Next: Copyright Keyword Searcher Click Here to Download Course Notes. Local Students Only. Direct link to Java Algorithm Animations (C related) Lecture notes + integrated exercises, solutions and marking Useful Links C tutorial for reference for beginners About this document ... Pointers. Let us now examine the close relationship between pointers and C's other major parts. We will start with functions. When C passes arguments to functions it passes them by value. There are many cases when we may want to alter a passed argument in the function and receive the new value back once to function has finished. Other languages do this (e.g. var parameters in PASCAL). C uses pointers explicitly to do this. The best way to study this is to look at an example where we must be able to receive changed parameters. swap(a, b) WON'T WORK.
Pointers provide the solution: Pass the address of the variables to the functions and access address of function. We can return pointer from functions. Pointers and arrays are very closely linked in C. Hint: think of array elements arranged in consecutive memory locations. WARNING: There is no bound checking of arrays and pointers so you can easily go beyond array memory and overwrite other things. a[i] can be written as *(a + i). i.e. A + i. pa[i] *(pa + i). Arrays and Strings. For two dimensions. For further dimensions simply add more [ ]: int bigD[50][50][40][30]......[50]; Elements can be accessed in the following ways: anumber=tableofnumbers[2][3]; tableofnumbers[25][16]=100; In C Strings are defined as arrays of characters. For example, the following defines a string of 50 characters: char name[50]; C has no string handling facilities built in and so the following are all illegal: char firstname[50],lastname[50],fullname[100]; firstname= "Arnold"; /* Illegal */ lastname= "Schwarznegger"; /* Illegal */ fullname= "Mr"+firstname +lastname; /* Illegal */ However, there is a special library of string handling routines which we will come across later.
To print a string we use printf with a special %s control character: printf("s",name); NOTE: We just need to give the name of the string. In order to allow variable length strings the 0 character is used to indicate the end of a string. Exercise 12335 Write a C program to read through an array of any type. Exercise 12336 or.