Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

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*)
0
rgb192
Asked:
rgb192
2 Solutions
 
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
 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now