pointer-course

path: courses/c-course/pointer-course.md

- **fileName**: pointer-course
- **Created on**: 2024-08-10 11:40:41

pointer and pointer for pointer : code example

#include <stdio.h>

int main() {
	int foo = 10;
	int *x; // creating pointer for int
	x = &foo; // & => for getting the pointer 
	int **z; // for poiter for a pointer
	z = &x;


	printf("1- the value: %d,  and address: %p\n", foo, &foo);
	printf("2- the value: %d,  and address: %p\n", *x, x);
	printf("3- the value: %d,  and address: %p\n", **z, z);
	return 0;
};

- with the same idea can make more pointers for pointers and all of them for the end point to the same address for value 10
- even when print the address for z is not like the address for x and foo but by becuase z is the pointer is pointing to x address and x address pointing ot foo so z is ponit to foo

why it's better to using a pointer for function parameter:

1- because when using pointer make doing operation in the parameter is more easy and not need for make another variable and save the values from the operation and then return the variable by using pointer it's gone change the variable that point to the address and make dirrect changes

example double array values:

#include <stdio.h>
#include <stdlib.h>

void doubleArray(int *foo, int size) {
	int i = 0;
	while( i <= size) {
		foo[i] = 2 * foo[i];
		i++; 
	}
};

int main() {
	int *foo = (int*)malloc(3*sizeof(int));
	foo[0] = 1;
	foo[1] = 2;
	foo[2] = 4;
	int size = sizeof(&foo) / sizeof(foo[0]); // 2
	doubleArray(foo, size);

	for (int i = 0; i <= size; i++) printf("the value is: %d\n", foo[i]);
	return 0;
};

creating a print function example:


#include <stdio.h>

void print(char *A, int size) {
    while (*A != '\0') {
        printf("%c ", *A);
        A++; 
    }
}

int main(int argc, char *argv[])
{
    char welcome[] = "Hello, World!";
    int size = sizeof(welcome) / sizeof(welcome[0]); // not using the size in the example
    // if not using a pointer for the function param we gone use the size but we use the pointer in this example (;
    print(welcome ,size);
    return 0;
}

multi dimensional array code example

#include <stdio.h>
int main(int argc, char *argv[]) {
    puts("welcome and nice too meeting you\n");
    int p[2][3] = {{1,23,3}, {4,5,7}}; // create a array with two arrays
    int *(u)[3]; // can't setting an pointer to the fulll array so i gone take i piece of an array and store
    for(int i = 0; i < 2; i++) 
        for(int j = 0; j < 3; j++) {
            u[i] = &p[i][j]; // saving an address an element
            printf("the address : %p, and value: %d\n", u[i], *u[i]);
        };
    return 0;
};

dynamic memory allocate code example malloc, calloc, realloc, free:

#include <stdlib.h>
int main() {
	int *foo = (int*)malloc(sizeof(int)); // create a variable for int 
	int *bar = (int*)malloc(10 * sizeof(int)); // in this creata an array of size 10
	bar[1] = 10;
	bar[2] = 11;
	bar[3] = 13;
	bar[4] = 12;


	/*
		 calloc it's like malloc but take the size like ( the number , and type)
		 - but there is an difference between malloc and calloc its when using malloc don't change the default value to 0
		 - calloc change the default value to zero
	 */
	int *foo2 = (int*)calloc(3, sizeof(int));
	
	/*
		realloc using for change the size for dynmaic memory locate
		realloc(memoryAdress , new size)
	*/
	int *foo3 = realloc(foo2, sizeof(int));
	
	/*
		for unlocate the reference for the adddress
		using the freek(address):
	*/
	free(foo);
	free(foo2);
	free(bar);
	return 0;
}

better way for handle with pointer is using the dynamic memory allocate because the variable life time inside a function it's end when the function finish call for this it's better using it for store the values in the head and choose when the address is setting to free and not causing any problem

#include <stdio.h>
#include <stdlib.h>

int *addNumbers(int* a, int* b) {
    int* c = (int*)malloc(sizeof(int));
    *c = (*a) + (*b);
    return c;
};

int main() {
    int a = 10, b = 20;
    int* ptr = addNumbers(&a, &b);
    printf("the sum: %d \n", *ptr);
    free(ptr);
    return 0;
}

continue:[[]]
before:stdlib.md