Uncategorized – My Blog http://beforebell.com/blog Learning through sharing Thu, 22 Sep 2022 11:19:09 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.2 How pointers are used in Parameter Passing in C? http://beforebell.com/blog/2022/09/22/how-pointers-are-used-in-parameter-passing-in-c/ http://beforebell.com/blog/2022/09/22/how-pointers-are-used-in-parameter-passing-in-c/#respond Thu, 22 Sep 2022 06:06:43 +0000 http://beforebell.com/blog/?p=52 Read MoreHow pointers are used in Parameter Passing in C?

]]>
MAKAUT BTech CSE/IT Data Structure concepts

Answer:

Accessing some memory locations remotely:

A pointer variable may be used to access some memory location that has been allocated for some other variable within or outside of its scope.

The variables defined within a function (remember, main() is also a function), are alive and active within the function itself. The code within which a variable is alive is called the scope of the variable. Thus all variables created within a function (including the formal parameters) have local or function level scope. 

The local variable of one function can not be accessed from other functions by its name. But, we can access the memory location allocated for that variable using its address. Hence, we may use a pointer variable to access the local variable of some function from another function, if needed. This concept can be further applied to parameter passing while function calls, File Handling, the creation of Linked Lists, Trees, and many more problem domains.

Explanation

A variable.

Example:

The following code snippet shows how to use a pointer.

#include<stdio.h>

void print_x(int x){   // This function creates a new variable named x  
    x = 200; 
    printf("\n\nvalue of x is %d", x);
}

void print_x_using_pointer(int *p){
    printf("\n\nvalue of x is %d", *p);
}

int main(){
    int x = 100;
  
    print_x();  // This will print 200    
    print_x_using_pointer(int *p);  // This will print 100   
    
    return 0;
}

Related Question:

  1. What is “call by reference” in C language?
  2. What is the scope of any variable?
  3. What are the “actual parameter” and the “formal parameter”?
  4. What is the scope of the formal parameters?
]]>
http://beforebell.com/blog/2022/09/22/how-pointers-are-used-in-parameter-passing-in-c/feed/ 0 52
How pointers can be used in C Programming? http://beforebell.com/blog/2022/09/20/how-pointers-can-be-used-in-c-programming/ http://beforebell.com/blog/2022/09/20/how-pointers-can-be-used-in-c-programming/#respond Tue, 20 Sep 2022 11:06:58 +0000 http://beforebell.com/blog/?p=39 Read MoreHow pointers can be used in C Programming?

]]>
MAKAUT BTech CSE/IT Data Structure concepts

Answer:

Pointers may be used in a code in two different ways.

1. Dynamic Memory Allocation:

For efficient memory management, we can allocate memory to store data at run-time. If we use an array, the size of the array is fixed at compilation time. So, we always try to create a significantly large array to store all possible data. This increases the wastage of memory as all of the memory, allocated for the array, may not be used at run-time (i.e., while execution). Using dynamic memory allocation, we can create (malloc() and calloc() functions) or remove (free() function) space for variables as and when needed.

Creation of a dynamic array:

// if n is a user input (integer), the following statement creates an array of length n.

int* array_ptr = (int *)malloc(n * sizeof(int));

// To delete the entire array

free(array_ptr); //deallocates memory


2. Parameter passing between functions (so-called Call-By-Reference):

When a function is called, the caller function can send the address of some variable to the called function. The called function must use a pointer to hold that address. Thus, the called function can access and update the memory location of the variable whose address has been passed to it. All the changes made by the called function are stored in the variable of the called function. So, when the called function returns (and is removed from memory), the changes it has made will persist.

Explanation

The following example has two functions to swap the values passed from the main function (caller function, in this case).

In swap(), the values of x and y from main() ara copied to the variables named a and b in swap(). All these variables are auto (automatic) variables and local in scope. So, although the values of a and b have been swapped within the swap(), the values of x and y in main() remain the same (see sample output).

In the case of swap2(), the function is having two pointer variables (p1 and p2). Though these two pointers are also local and auto in nature, but these pointers are set with the addresses of x and y, respectively, from the caller function (main() in this case). So, the swap operation performed through p1 and p2 is actually performed in the memory locations of x and y. Thus, the result of swap2() will persist even when the function ends.

Example:

The following code snippet shows how to use pointers as a formal parameter.

#include<stdio.h>

void swap(int a, int b) {     // Call by value

	int temp;
	temp = a;
	a = b;
	b = temp;
	return;
}

void swap2(int *p1, int *p2){  // Call by reference

	int temp;
	temp = *p1;
	*p1 = *p2;
	*p2 = temp;
	return;
}

int main(){
    int x = 5, y = 10;
    printf("\n Before swap()   : x = %d, y = %d", x, y);    
    swap(x, y);
    printf("\n After swap()    : x = %d, y = %d", x, y);
    swap2(&x, &y);
    printf("\n After swap2()   : x = %d, y = %d", x, y);       
    return 0;
}
sample-output-c-programming

Related Question:

  1. Can we reuse the same pointer variable to point to some other array after deleting the previously allocated dynamic array?
  2. What happens if we do not use the free() after using the dynamically allocated array?
  3. Differentiate between call by value and call by reference in C language.
  4. Is call by value and call by reference almost the same in C language?
  5. Why the changes made in “call by value” do not persist?
  6. How much memory overhead is there in “call by reference” over “call by value” C language??
]]>
http://beforebell.com/blog/2022/09/20/how-pointers-can-be-used-in-c-programming/feed/ 0 39
Hello world! http://beforebell.com/blog/2022/08/31/hello-world/ http://beforebell.com/blog/2022/08/31/hello-world/#respond Wed, 31 Aug 2022 10:37:44 +0000 http://beforebell.com/blog/?p=1 Read MoreHello world!

]]>
Welcome to my blog site. I shall post various updates and materials here. I hope this will be helpful for young school and college students.

Please feel free to contact me with any queries. By the way, I am always open to suggestions. 🙂

]]>
http://beforebell.com/blog/2022/08/31/hello-world/feed/ 0 1