• C

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*)
LVL 1
rgb192Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
(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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kent OlsenData Warehouse Architect / DBACommented:
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!
Kent
0
rgb192Author Commented:
so casting allows a datatype change and Every variable is declared to contain a specific type of data.
Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.