290 1
Write a C program for the following pyramid * * * * * *
  • * * * * * *
  • * * * * *
  • * * * *
  • * * *
  • * *
  • *
  • * *
  • * * *
  • * * * *
  • * * * * *
  • * * * * * *
...
Type
Question
654 1
Not understanding what happens in memory during dynamic memory allocation using pointers in C?

#include<stdio.h>

#include<stdlib.h>

int main()

{

    int *p;

    p=(int *) malloc(4);

    if(p==NULL)

    {

        printf("Insufficient memory");

        return;

 

    }

    printf("Enter a number\n");

    scanf("%d",p);

 

    printf("Value of p=%d",*p);

 

}

 

1) What happens in background when we do int *p? Can you show a figure about what it does in memory?

 

2) What happens in memory "p=(int *) malloc(4);" when we run this line? In surface, I've rote memorized, it allocates 4 bytes of memory, but I don't know in detail what happens in background.

 

3) printf("Enter a number\n");

    scanf("%d",p);

 

I need to store a number in address of p, so should not I do &p instead of just p?

 

4) Value of p=p isn't it? Why *p? I've again rote memorized *p gives value of p, but I'm not clear about what's going inside memory during this all.

 

I'm asking these questions knowing I'm wrong because I've tested the code in codeblocks already. It's not to get the correct answer but a way to think to get a correct answer.

 

If we'd done;

int x=10;

int *p=&x;

I'd understand what's being done here.

It means content of p is initialzed with address of x. Like this:

https://imgur.com/a/7BNzsGO

but I don't understand the above case that I mentioned in question where we are only doing int *p. I find it meaningless. p is a pointer variable. But it contains whose address?

...
Type
Question
2308 0
Switch case in C++ with example
In this article, I have provided switch case c++ example and how to use switch statement in c++
Type
Article
6301 0
Addition program in C ( Adding two integers)
In this article, I have explained line by line and provided code for the c program to add two numbers.
Type
Article
6830 0
Sorting linked list program in C
This article provides you source code with the sample output of entering nodes in a linked list and then sorting a linked list in C.
Type
Article

12730 0
Bubble sort algorithm in C (With sample program)
This article provides introduction to bubble sort, bubble sort algorithm in C with sample program for bubble sort program in C.
Type
Article
6675 0
Stack Program in C (Concept, Algorithm & C program example)
This article provides you concept, algorithm & code for stack program in C with push, pop & display operation.
Type
Article
7375 0
Program for insertion sorting in C (With explanation)
This article provide you to understand the logic & code of insertion sort program in C with explanation.
Type
Article
10611 0
Program & algorithm for Quick sort in C
This article provides you a brief explanation for quicksort and also gives your algorithm and program code for quicksort in C
Type
Article
9513 0
Merge sort algorithm in C with Program sample
In this article, I will provide your merge sort algorithm in C with sample program and output for it.
Type
Article

Page 1 of 4