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
What is a pointer variable in C? http://beforebell.com/blog/2022/09/20/what-is-a-pointer-variable-in-c/ http://beforebell.com/blog/2022/09/20/what-is-a-pointer-variable-in-c/#respond Tue, 20 Sep 2022 08:16:02 +0000 http://beforebell.com/blog/?p=25 Read MoreWhat is a pointer variable in C?

]]>
Exams:

MAKAUT BTech CSE

Answer:

A pointer is a variable that stores the address of some other variable, instead of data. An integer variable can store some integer value, but an integer type pointer variable can only hold the address of an integer variable. We can also access and change the value of the integer variable using the pointer.

Explanation

The “data” stored within a pointer variable is considered as an “address”.

“37, M. N. Sen Street, Kolkata 700001 ” – Is it an address or data?

When we see the above text on an envelope, the text is considered to be an address. But, if we see the same text on someone’s driving license or Adhar Card, the same text is considered to be data. Similarly, The data stored within a pointer variable is a Hexadecimal number which represents the address of some variable.

Example:

The following code snippet shows how to use a pointer.

int main(){
    int x;  // creating an integer
    int *p; // creating a pointer to integer variable
    x = 5;
    p = &x;
    printf("x contains %d at location %x", x , &x);
    printf("\n");
    printf("p contains %x at location %x", p , &p);
    printf("\nAccessing x via p:-\n");
    printf("value of x is %d", *p);
    return 0;
}
sample-output

Related Question:

  1. What is the address of any variable?
  2. Can a pointer variable store values?
  3. What type of values can be stored in a pointer variable?
  4. Can we perform addition or subtraction on pointers?
  5. What is the use of * while using pointers in C?
]]>
http://beforebell.com/blog/2022/09/20/what-is-a-pointer-variable-in-c/feed/ 0 25
What is the return type of malloc()? http://beforebell.com/blog/2022/09/17/what-is-the-return-type-of-malloc/ http://beforebell.com/blog/2022/09/17/what-is-the-return-type-of-malloc/#respond Sat, 17 Sep 2022 18:41:15 +0000 https://beforebell.com/blog/?p=9 Read MoreWhat is the return type of malloc()?

]]>
Exams:

MAKAUT BTech CSE, 2017 (Marks: 1)

Answer:

void* (void type pointer).

Explanation

The malloc() is used to allocate memory at run-time. This is called dynamic memory allocation.

Both malloc(), and calloc() are used to get a chunk of free memory from the Operating System. These functions return the starting address of such a chunk or block of memory. The program code needs to typecast this chunk as per it’s needs.

Example:

The following code snippet shows how a node is created for a singly linked linear list.

// Code to define a simple node structure for a Singly Linked Linear List
struct NODE{
char data;
struct NODE *ptr;
}
typedef struct NODE node;

// Function to create a new node. The node is loaded with the given value in 
// the data field and Null in the link field by default.
node* createNode(char value){
    node* temp;
    temp = (node *)malloc(sizeof(node));
    if( temp==NULL )
        return NULL;
    temp->data = value;
    temp->ptr = NULL;
    return temp;
}

Related Question:

  1. What is the return type of calloc()?
  2. Why do we need to typecast the return value of malloc()?

]]>
http://beforebell.com/blog/2022/09/17/what-is-the-return-type-of-malloc/feed/ 0 9
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