#include <stdio.h>
#include<stdlib.h>
/*
//Lab 2
//********ARRAY***************
//*** Array to store the muiptle variables in the single variables
//Syntax; Datatype arrayname[Length_of_the_ array]= (element1 and element2,....);
//***********POINTER***************
//*****POINTER is used to store the memory
//***Dynamic Memory Allocation
//**int arr[5]; not able to change the size of the array after ceration.
//A function malloc(), calloc(); free(), realloc()
//malloc( used to dynamically allcoate the single block of memory with the specific size.
//Syntax: datatype * pointerName = (cast_type*)mallo(size_int_bytes);
/*
cast_type,
calloc() – contiguous allocation is used to dynamically allocate the specified
number of blocks of memory with the specified type.
- Syntax: ptr = (cast_type*) calloc(n, element_size_in_bytes);
realloc() – re-allocation is used to dynamically change the memory allocation of a
previously allocated memory.
- Syntax: ptr = realloc(ptr, new_size);
free() – delocate or releasing the memory, reduce the wastage of memory.
- Once memory is no longer needed, we must release it using free() function
- Syntax: free(ptr);
*/
void update(int * ptr){
* ptr = 30;
}
int main() {
int size = 5;
int* ptr = (int*)malloc(size*sizeof(int));
//ptr to the first element
//*ptr the value of the 1st element
for(int i=0;i<size;i++){
scanf("%d", (ptr+i));
}
for(int i=0;i<size;i++){
printf("%d, \n", *(ptr+i));
}
free(ptr);
//int A = 21;
// int * ptr = &A;
// update(&A);
//printf("%d\n", A);
//ptr is used the store the addree of A
//*ptr is used to store the value of A
//printf("Address of A using the pointer" %p\n, ptr);
//printf(Value of A using a pointer: %d,*ptr);
//int arr[5] = { 25, 30, 59, 65, 100};
// int* ptr = arr;
// printf("%d\n", *(ptr + 1)); // Outputs 2
// for(int i = 0; i < 5;i++){
// printf(" %d \n", *(ptr + i);
//int arr[5] = {25, 30, 59, 65, 100};//index is 0, 1, 2, 3, 4.
//Last index < the length_of_array
//printf(" %d \n", arr[1]);
//Display all the elements of the array.
//for(int i = 0; i < 5;i++){
// printf(" %d \n", arr[i]); //i=0 => 25, 1 = 1 => 30,.....
printf("Hello world!\n");
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: