How to use Array of integer Pointers in C

Array of pointers to integers.i.e.Array of integer pointers.Since,Integer pointer store address of one integer.Therefore,Array of pointers to integers will store addresses many integers.
#include<stdio.h>
int main()
{
    int a[]={10,20,30};/*array of integers*/
    int* arr[]={a,a+1,a+2};/*array of integer pointers so contains addresses*/
 
    printf("\nAddresses of integers:\n");
    printf("address of a=%u\n",a);/*no & needed as name of the array gives base address.*/
    printf("address of a+1=%u\n",(a+1));
    printf("address of a+2=%u\n",a+2);

    printf("\nAddrs of integers are stored in arr so each arr[i] is an address:\n");
    printf("arr[0]=%u\n",arr[0]);
    printf("arr[1]=%u\n",arr[1]);
    printf("arr[2]=%u\n",arr[2]);

Array of Pointers to integer in C

/*Array of pointers to integers i.e. Array of integer pointers. Since integer pointer store one address.So,array of integer pointers is used to store  many addresses .*/
#include<stdio.h>
int main()
{
    printf("\nBefore starting array of pointers Understand pointer \n");
    printf("\nint *p; tells to compiler that p will be used to store single address of integer\n");
    int *p;//p will be used to store address of a pointer
    int i=10;
    p=&i;//pointer is used to store address of int i
    printf("address of i=%u or p=%u\n",&i,p);
    printf("value of i=%d or *p=%u\n",i,*p);


    printf("\nNow understanding array of pointer to integers.\n");
    printf("\narray of pointers to integers is used to store many addresses of integers\n");
    int a=10,b=20,c=30;
    int* arr[]={&a,&b,&c};/*arr is array of integer pointers so contains addresses*/
 
    printf("\nAddresses of integers:\n");
    printf("address of a=%u\n",&a);
    printf("address of b=%u\n",&b);
    printf("address of c=%u\n",&c);

Circular Queue, Circular linked list

/*linked list as circular queue*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *next;
};

void addq(struct node **f, struct node **r, int d);
void deleteq(struct node **f, struct node **r);
void display(struct node *f);

Singly linked list all operations by passing head

/*Code for:
  linked list through head
  1.insert at beginning
  2.inser after given position
  3.inser at end/append
  4.display
  5.reverse
  6.delete from beginning
  7.delete the given node value
  8.countnumber of nodes
 */
#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *next;
};
struct node *head;//*head is start

void insertBegin(struct node **head, int d);
void insertPosition(struct node **head, int position, int data);
void insertEnd(struct node **head, int d);
void display(struct node *head);
void count(struct node *head);
void reverse(struct node **head);
void deleteBeginning(struct node **head);
void deleteKey(struct node **head);

Linked list all operations without head pass in c

/*   code for single linked list operations:
   1.insert at Begin
   2.insert at Last//append
   3.Insert at Given Position
   4.Display
   5.reverse
   6.delete at beginning
   7.delete given node
 */
#include<stdio.h>
#include<stdlib.h>

struct node
{
    int data;
    struct node *next;
};
struct node *start;

void insertAtBegin(int d);
void insertAtLast(int d);
void insertAtPosition(int d,int p);
void display();
void reverse();
void deleteAtBegin();
void deleteKey();
void count();

Dynamic Memory Allocation

C Programs  for dynamic memory allocations in C
/*code for One Dimensional array dynamic memory allocaion*/
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 5

int main()
{
int *ptr;
ptr=(int *)malloc(MAXSIZE*sizeof(int));

int i;
for(i=0;i<MAXSIZE;i++)
{
printf("ptr[%d]=%d \n",i,i);
}

printf("\n");

return 0;
}
/*
ptr[0]=0
ptr[1]=1
ptr[2]=2
ptr[3]=3
ptr[4]=4
*/

/*
code for 2 D alternate dynamic allocation
*/

#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOLUMN 4

int main()
{
int *ptr,i,j;
ptr=(int *)malloc(MAXROW*MAXCOLUMN*sizeof(int));

for(i=0;i<MAXROW;i++)
{
for(j=0;j<MAXCOLUMN;j++)
{
printf("%d+%d=(%d) ",i,j,i+j);
}
printf("\n");
}
printf("\n");
return 0;
}
/*
0+0=(0) 0+1=(1) 0+2=(2) 0+3=(3)
1+0=(1) 1+1=(2) 1+2=(3) 1+3=(4)
2+0=(2) 2+1=(3) 2+2=(4) 2+3=(5)

*/



/*
code for 2-Dimensional array
*/

#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOLUMN 4
int main()
{
int **ptr,i,j;

ptr=(int **)malloc(MAXROW*sizeof(int *));

for(i=0;i<MAXROW;i++)
{
ptr[i]=(int *)malloc(MAXCOLUMN*sizeof(int ));
}


for(i=0;i<MAXROW;i++)
{
for(j=0;j<MAXCOLUMN;j++)
{
printf("%d+%d=(%d) ",i,j,i+j);
}
printf("\n");
}

return 0;
}
/*
0+0=(0) 0+1=(1) 0+2=(2) 0+3=(3)
1+0=(1) 1+1=(2) 1+2=(3) 1+3=(4)
2+0=(2) 2+1=(3) 2+2=(4) 2+3=(5)
*/



/*
dynamic memory allocation for 3-Dimensinal array
*/
#include<stdio.h>
#include<stdlib.h>
#define MAXX 2
#define MAXY 3
#define MAXZ 4
int main()
{
int ***ptr,i,j,k;
ptr=(int ***)malloc(MAXX*sizeof(int **));

for(i=0;i<MAXX;i++)
{
ptr[i]=(int **)malloc(MAXX*sizeof(int *));
for(j=0;j<MAXY;j++)
{
ptr[i][j]=(int *)malloc(MAXY*sizeof(int));
}
}

for(i=0;i<MAXX;i++)
{
for(j=0;j<MAXY;j++)
{
for(k=0;k<MAXZ;k++)
{
//printf("%d ",i);
printf("%d+%d+%d=(%d) ",i,j,k,i+j+k);
}
printf("\n");
}
printf("\n");
}

return 0;
}
/*
//when i
0 0 0 0
0 0 0 0
0 0 0 0

1 1 1 1
1 1 1 1
1 1 1 1


//when i+j+k
0+0+0=(0) 0+0+1=(1) 0+0+2=(2) 0+0+3=(3)
0+1+0=(1) 0+1+1=(2) 0+1+2=(3) 0+1+3=(4)
0+2+0=(2) 0+2+1=(3) 0+2+2=(4) 0+2+3=(5)

1+0+0=(1) 1+0+1=(2) 1+0+2=(3) 1+0+3=(4)
1+1+0=(2) 1+1+1=(3) 1+1+2=(4) 1+1+3=(5)
1+2+0=(3) 1+2+1=(4) 1+2+2=(5) 1+2+3=(6)
*/

….


-----

/*
code for dynamic memory allocation for structure
*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int empid;
char *name;
};
int main()
{
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->name=(char *)malloc(sizeof(30));
newnode->empid=1020;
newnode->name="Aditya";

printf("\nempid=%d and name=%s\n\n",newnode->empid,newnode->name);

newnode->empid=1021;
newnode->name="Birla";
printf("\nempid=%d and name=%s\n\n",newnode->empid,newnode->name);

return 0;
}
/*
empid=1020 and name=Aditya

empid=1021 and name=Birla
*/

/*
code Showing PROBLEM of returning local array address and solution
1. static array
2. dynamic memory alllocation
*/

#include<stdio.h>
#include<stdlib.h>//malloc
#include<string.h>//strcpy
char* func1();
char* func11();
char* func2();

int main()
{
char *ptr1;
ptr1=func1();
printf("func1 returnd: %s\n",ptr1);//prints some GARBAGE as array of func1 was local/auto

char *ptr11;
ptr11=func11();
printf("func11 returnd: %s\n",ptr11);//prints Hello India11


char *ptr2;
ptr2=func2();
printf("func2 returnd: %s\n",ptr2);//prints Hello India 2


printf("\n\n");
return 0;
}
/*PROBLEM with returning local array*/
char* func1()
{
char buff1[20];//local array
//buff1="Hello India1";/*error:incompatible types when assigning to type âar[20]ârom type âar *â*/
strcpy(buff1,"HEllo India1");//warning
return (buff1);//warning returning local array is not ok
}

/*SOLUTION 1: use static array*/
char* func11()
{
static char buff11[20];//istatic local array
strcpy(buff11,"HEllo India11");//warning
return (buff11);//returning static local array is okay
}

/*SOLUTION 2: dynamic memory allocation*/
char* func2()
{
char *buff2;
buff2=(char *)malloc(20);
buff2="Hello India2";
return (buff2);/*returning dynamically allocated memory is okay*/
}
/*
func1 returnd: á/c· ú¿
func11 returnd: HEllo India11
func2 returnd: Hello India2
*/


/*
Difference between:
1. char p1[5] //Array of characters
2. char* p2[5] //Array of character pointers
3. char (*p3)[5] //Pointer to Array of character
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int *ptr[10];//ptr is array of 10 integer pointers
printf("\nptr[0]=%p\n",ptr[0]);//0x8049ff4

int arr[10]={0,1,2,3,4,5,6,7,8,9};

//assgining ptr[0] with the address of arr[0]
*ptr=arr;
//*ptr=arr[0];//warning: assignment makes pointer from integer without a cast
printf("\n ptr[0]=%p \n arr[0]=%p\n",ptr[0],arr);//0x8049ff4

/*with characters */

char p1[5];//p1 is 5-element array of chars
char *p2[5];// p2 is 5-element array of char *
char (*p3)[5];//p3 is pointer to 5-element array of chars

strcpy(p1, "Hello");

p2[0] = (char *) malloc(5 * sizeof(char));
strcpy(p2[0], "India is Great");

p3 = &p1;

printf("\n p1 = %s\np2[0] = %s\np3 = %s\n", p1, p2[0], *p3);
printf("\n");
return 0;
}
/*
___________
p1 |h| e | l | l | o |
---------------


"adi" "mahi" "soni" "jaya" "bangalore"
^          ^        ^           ^      ^
|            |          |            |        |
____________________________________
|0x9999|0x7777|0x3333|0x2222|0x6666|
|___0__|___1__|___2__|___3__|___4__|
p2




________ _____________________
|0x7777| -------> | H | e | l | l | o |
|______| |_0_|_1_|_2_|_3_|_4_|
p3 ^
|
0x7777
*/
/*
ptr[0]=0xb7765fdc

ptr[0]=0xbfe81c34
arr[0]=0xbfe81c34

p1 = Hello
p2[0] = India is Great
p3 = Hello

*/