Please enable JavaScript.
Coggle requires JavaScript to display documents.
pointers and string functions - Coggle Diagram
pointers and string functions
Declaration
int *p; int head=5; p =& head;
Displaying pointer value
printf("value of p is %p",p); printf("value of head is %d",*p);
Pointer Arithmetic
using ++/-- you may iterate or decrement through an array however the array name itself is a pointer constant and cannot be used
Assigning array pointer to a pointer variable
int &ptr; int Numbers[25] = {1,2,3,4,5,6}; ptr = Numbers;
may throw an out of bounds error
Functions
3Steps
Declare prototype
tells compiler value returned, number of parameters, parameter type.
function call
executation, normally within main however does not have to be
definition
function name parameters & datatypes, contains function statments and return types. if no return type then the function is defined as void.
Illustration
Passing variables
pass by value
no change to varible, passes a copy of the actual variable(int float...)
pass by reference
when you wish to see if the variable has been changed by the function. must pass the variable address, by default arrays are passed by reference in order to prevent this use const in the array declaration
program e.g.
Variable contents before function call
variable in main block
[num1 &num1 = x:01][num2 &num2 = x:02]
num1 = 8
num2 = 5
variables in function block
:[temp &temp = x:03]
variable contents after function call
variable in main block:
[num1 &num2 = x:01][num2 &num = x:02]
num1 = 5
num2 = 8
variables in function block
[temp &temp =x:03] ptr &ptr1 = x:04
temp = 8 , ptr1 = x:01 ptr2 = x:02
Strings
declaration
CharName[SIZE]
Getting input
scanf("%s",input);
fgets(name,SIZE, stdin);
output using pritnf("%s",string), puts(string); & fputs("hello world",stdout);
common functions
include<stdio.h>
len = strlen(string)
outputs string length. does not count the last char i.e '\0'
strcpy(destination ,source)
Concatenate
strcat(str1,str2)
appends string str2 to end of str1
Make sure STR 1 is big enough to hold the new string.
Compare
strmp(srt1,srt2)
compares two null terminated string, and returns.
zero if STR1 == STR2
returns a negative value, if STR1 < STR2
returns a positive value, if STR1 > STR2
finding first occurrence of a char in string
strchr(str,ch)
find the first occurrence of ch in str
returns
pointer to ch
finding last occurrence of a char in string
strchr(str,ch)
Double pointers
A pointer can also point to another pointer, which in turn points to a standard variable
e.g
pointer
int *j;
int var
int i = 3;
pointer to pointer(double pointer)
int **k;
assingment
assign address of pointer
k=&j
assign address of int
j =&i;