Why Need to cast??!!

This is a discussion on Why Need to cast??!! within the Game Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #define min_range 0 #define max_range 100 void exchange(int *x, int *y) { int t = *x; *x ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    77

    Why Need to cast??!!

    Code:
    #include <stdio.h>
    #define min_range 0
    #define max_range 100
    
    void exchange(int *x, int *y)
    {
    	int t = *x;
    	*x = *y;
    	*y = t;
    }
    
    void quick_sort(int *a, int p, int r)
    {
    	int q;
    	if (p < r) {
    		q = partition(a, p, r);
    		quick_sort(a, p, q-1);
    		quick_sort(a, q+1, r);
    	}
    }
    
    int partition(int *a, int p, int r)
    {
    	int x = a[r];
    	int j, i = p-1;
    	for (j = p; j < r; j++) {
    		if (a[j] <= x) {
    			i++;
    			exchange(&a[i], &a[j]);
    		}
    	}
    	exchange(&a[++i], &a[r]);
    	return i;
    }
    
    void generation(int *a, int n)
    {
    	FILE *fData;
    	int i;
    	srand(time(NULL));
    	fData = fopen("quick-sort.dat", "wt");
    	for (i = 0; i < n; i++)
    		a[i] = rand()%(max_range-min_range+1)+min_range;
    	fprintf(fData, "%d\n\n", n);
    	for (i = 0; i < n; i++) {
    		if ((i % 10 == 0) && (i > 0)) fprintf(fData, "\n");
    		fprintf(fData, "%d ", a[i]);
    	}
    	fclose(fData);
    }
    
    void outputData(int *a, int n)
    {
    	FILE *fData;
    	int i;
    	fData = fopen("quick-sort.out", "wt");
    	for (i = 0; i < n; i++) {
    		if ((i % 10 == 0) && (i > 0)) fprintf(fData, "\n");
    		fprintf(fData, "%d ", a[i]);
    	}
    	fclose(fData);
    }
    
    int main()
    {
    	int *a;
    	int i, size;
    	printf("size = ");
    	scanf("%d", &size);
    	a = malloc(size*sizeof(*a));
    	generation(a, size);
    	quick_sort(a, 0, size-1);
    	outputData(a, size);
    	return 0;
    }
    Code:
    quick-sort.c: In function `main':
    quick-sort.c:71: warning: assignment makes pointer from integer without a cast
    I have read CProgramming F&Q. In this case, i need not to cast pointer a. Why there has warning about casting?!

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Probably because you are using a C++ compiler not C.
    Woop?

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,199
    try including stdlib.h

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including The Right DLLs
    By bumfluff in forum Game Programming
    Replies: 8
    Last Post: 12-28-2006, 02:32 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Converting Double to Float
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2006, 02:46 PM
  4. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 09:14 PM
  5. errors in class(urgent)
    By ayesha in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2001, 05:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21