Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.

Similar presentations


Presentation on theme: "Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers."— Presentation transcript:

1 Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers

2 Pointers pointer to constant value constant pointer to value
const int * p p can’t change the value of variable it is pointing to constant pointer to value int* const p = &j; p can’t point to anything else, but j.

3 Characters and Strings
A character in ASCII set is represented by integers from 0 – 255. char data type is used to represent that ASCII set of characters char ch; ch = ‘c’; // this is equivalent to ch = 99 because in ASCII table, ‘c’ character’s value is 99. %c, scanf(“ %c”) to force scanf skip any white space before char getchar(), returns an int as read from console char and ints interchangeable (betwee 0 – 255)

4 Characters and Strings
String Literals sequence of characters enclosed in double quotes. C treats them as character arrays When C compiler encounters string literal, it allocates memory to store characters of the string plus one extra character to mark end of string. This special character is called the null character. It is represented as first character in ASCII table, i.e. its value is 0 and it is represented as ‘\0’ as character. The ‘0’ character representing a zero is not same as null character (i.e. the first ASCII character). The ‘0’ character has ASCII value 48. So, always remember the difference between null character (i.e. ‘\0’) and character zero (i.e. ‘0’)

5 Strings Storing strings char msg[8] = “message”; char msg[8] = “me”;
use character arrays size should be alteast one more that the number of characters in the string. the last character should be null. char msg[8] = “message”; msg[] => ‘m’, ‘e’, ‘s’, ‘s’, ‘a’, ‘g’, ‘e’, ‘\0’. char msg[8] = “me”; msg[] => ‘m’, ‘e’, ‘\0’ char msg[] = “message”;

6 String char *ptr; ptr = “This is a text”; ptr = “this is new text”;
Compiler allocates required memory. read-only. ptr = “this is new text”; this is fine. the pointer can be used to point to other string literal. Reading strings gets(str) reads characters including whitespace from console. str should be allocated memory by user so, if str is char str[100], it works, but if str is char *str, it wont’.

7 string.h library functions for string manipulations
strlen() to find the length of the string. It has to be null terminated. size_t strlen(const char* str) strcpy() to copy characters of one string to other char * strcpy(char *dest, const char* src); dest should have enough memory allocated to fit string pointed to by src strncpy() strcmp returns 0 if two string match literally

8 Functions Variables scope Global variables static variables
arrays as arguments void test (int arr[]) void test (int arr[][10]) treated as pointer – so no copy of actual array is made pass length as argument for function to know about the array’s size. functions with variable number of parameters void test (int num, char *str, ….); atleast one fixed parameter is must recursive functions

9 Pointer to functions return_type (*pointer_name) (argument list…)
int (*ptr) (int a); used in callback functions array of pointers to functions void (*ptr[20]) (int a)


Download ppt "Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers."

Similar presentations


Ads by Google