pointer typecasting

Posted on 2014-08-21
Last Modified: 2014-08-31
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];
	//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
I do not undertand (int*)
Question by:rgb192
    LVL 84

    Accepted Solution

    (int*) realloc(NULL, n*sizeof(int))
    casts the void pointer returned by realloc into an int pointer,
    which makes it compatible with the int pointer B

    (int) 4.01
    would cast the double 4.01 into the int 4
    LVL 45

    Assisted Solution

    Hi rgb,

    Adding to ozo's explanation....

    A pointer is simply a memory address.  Its size depends on the machine architecture, but in today's world it is almost always a 64 or 32 bit value.  Older machines using 16 bits, and some were non-standard and used other sizes.  For programming purposes, the size really doesn't matter unless you're trying to use the pointer as something other than a pointer.

    Every variable is declared to contain a specific type of data.

      int integer_name;
      float float_name;
      char char_name;
      char *array_name;
      char *char_pointer_name;
      int   *int_pointer_name;
      int   *float_pointer_name;

    Open in new window

    Most of the reason for variables being declared to a specific data type is to prevent programming errors.  If a function parameter is declared as an integer, then C will only allow data that is an integer type to be passed.  If the function has an array parameter, then only the address of an array of the same type as the array can be passed.

    So not only can you not pass a character array to a function that is expecting an integer, you can't pass a character array when an integer array is expected.

    C has a special type called 'void'.  The void type indicates that the program isn't associating the value with any specific data type.  

     void *void_pointer;
      char *char_pointer;
      int  *int_pointer.
      void void_name; 

    Open in new window

    Line 1 in that block declares a pointer, but doesn't associate the pointer with any particular data type.  Line 2 declares a pointer to a character.  Line 3 declares a pointer to an integer.  Line 4 is illegal.

    When you set a pointer value, the program must honor the data type.

      char_pointer = int_pointer;

    That should throw an error.  

      char_pointer = void_pointer;

    Depending on your compiler options, that may be just fine, issue a warning, or throw an error.  You're seeing the effects of that assignment when you call malloc.

      void_pointer = malloc (1);
      char_pointer = malloc (1);

    Since malloc returns a (void*), the first line should always compile cleanly.  The intent of the second line is clear, but because the program is trying to implicitly recast the pointer the compiler may deem it illegal.  (Compiler options often allow implicit recasting of (void*) to other data types.)

    However, explicit recasting is almost always allowed.

      char_pointer = (char*)malloc (1);

    That should work just fine.  If the pointer type is (int*)

      int_pointer = (int*)malloc (1);

    In both cases, malloc returns a pointer.  The programmer explicitly recasts the pointer to match the type of the variable where it will be stored.  The value doesn't change.  The program simply declares its usage to be consistent with the variable.

    Good Luck!

    Author Closing Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
    The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now