C Programming – My Blog http://beforebell.com/blog Learning through sharing Tue, 20 Sep 2022 09:07:56 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.3 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