Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

pointer typecasting

#include<stdio.h>
#include<stdlib.h>
int main()
{
	int n;
	int aj;
	int aj2;
	int bj;
	printf("Enter size of array\n");
	scanf_s("%d", &n);
	int *A = (int*)malloc(n*sizeof(int));//dynamically allocated array
	for (int j = 0; j < n; j++)
	{
		aj2 = j + 1;
		A[j] = j + 1;
		aj = A[j];
	}
	//free(A);
	//A[1] = 3; //value at address A+1;
	int *B = (int*)realloc(NULL, n*sizeof(int)); //equivalent to malloc
	printf("Prev block address=%d, new address=%d difference=%d\n", A, B,(B-A));
	for (int j = 0; j < n; j++)
	{
		bj = B[j];
		printf("%d ", B[j]);
	}
}

Open in new window



I am doing self study, copying code from tutorial.

      int *B = (int*)realloc(NULL, n*sizeof(int)); //equivalent to malloc
I do not undertand (int*) typcasting
would (int) force a number where there is a decimal
4.01=4
I do not undertand (int*)
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

so casting allows a datatype change and Every variable is declared to contain a specific type of data.
Thanks.