assignment make pointer from intenger without a cast

return makes pointer from intenger without a cast

Page 1 of 1

1 Replies - 4409 Views - Last Post: 02 July 2008 - 03:55 PM Rate Topic: -----

#1 BSmith4740  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-June 08

assignment make pointer from intenger without a cast

Posted 02 July 2008 - 02:49 PM


 #include <stdio.h>
  2 #include <stdlib.h>
  3 /* function prototypes */
  4 int printMenu();
  5 void printArray ( int [ ], int );
  6 int addInt ( int [ ], int, int);
  7 int delINT ( int [ ], int, int);
  8 int exists ( int [ ], int, int);
  9 void insertSort ( int [ ], int );
 10 
 11 /* main begins program execution  */
 12 int main ( void )
 13 {
 14 
 15		 int *array = NULL;
 16		 unsigned int size = 0;
 17		 int num, opt, pos;
 18		  
 19		 /* START DO STATEMENT */
 20		 do {  opt = printMenu();
 21				 
 22				 /* begin switch statement */
 23				 switch (opt) {
 24				   /* The user selected to add an intenger */
 25				   case 1:  /* Prompts for the intenger and reads it */
 26							printf("\nPlease enter the intenger you wish to add to the array\n");
 27							scanf("%d",&num);
 28 
 29							/*  Add number to the array */
 30							/* Checks if the operation had success */
 31							if ((array = addInt(array, num, size)) != NULL )
 32							size++;
 33							else 
 34							  printf("Can't add %d to array",num);
 35 
 36							printArray(array, size);
 37 
 38							break;
 39  
 40 
 41 
 42		 }
 43		 }  /* END DO */
 44		 while ( opt != 0 );
 45 
 46 
 47 
 48 return 0;
 49 }
 50 
 51 /* function to print the menu */
 52 int printMenu (void)
 53 {
 54 int opt;
 55 
 56 printf("Enter 1 to add an intenger\n");
 57 printf("Enter 2 to delete an intenger\n");
 58 printf("Enter 3 Print Array\n");
 59 printf("Enter 0 - Exit\n");
 60 scanf("%d",&opt);
 61 return opt;
 62 }
 63 
 64 /* function to print the array */
 65 void printArray ( int array [ ], int size )
 66 {
 67 
 68 int i;
 69 
 70 /* Check if array has number */
 71 if (size)
 72 {
 73   printf("Numbers in array:");
 74 
 75   for ( i = 0; i < size; i++ )
 76   printf("%d\n", array [ i ] );
 77 }
 78 else
 79   printf("Array is empty!");
 80 
 81   printf("\n\n");
 82 }
 83 
 84 /* function to add in to array */
 85 
 86 int addInt ( int array [ ], int num, int size )
 87 {
 88	int *temp = NULL;
 89	int i;
 90 
 91	/* Copies the original array to the new array */
 92 
 93	for ( i = 0; i < size - 1; i++ )
 94	  temp [ i ] = array [ i ];
 95 
 96	 /*  Inserts the new element on the top of the array */
 97 
 98	 temp [ size - 1 ] = num;
 99 
100	 /* returns the temp array */
101 
102	 return temp;
103 }




warning: assignment makes pointer from integer without a cast
.c:17: warning: unused variable ‚pos‚
.c: In function ‚addInt‚:
.c:102: warning: return makes integer from pointer without a cast

This is the first time I have encountered this. The program fails to a segmentation fault. I am trying to add number to an array, delete intengers from the array, sort the array, print the array.

I have searched the internet for the warning and found multiple programs but none of them really explain how to fix this warning or what is going on to fix the warning. I will put it this way. I didn't understand what fixes this if I encountered it.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: assignment make pointer from intenger without a cast

#2 Einherjar  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 10-February 08

Re: assignment make pointer from intenger without a cast

Posted 02 July 2008 - 03:55 PM

View PostBSmith4740, on 2 Jul, 2008 - 05:49 PM, said:


 #include <stdio.h>
  2 #include <stdlib.h>
  3 /* function prototypes */
  4 int printMenu();
  5 void printArray ( int [ ], int );
  6 int addInt ( int [ ], int, int);
  7 int delINT ( int [ ], int, int);
  8 int exists ( int [ ], int, int);
  9 void insertSort ( int [ ], int );
 10 
 11 /* main begins program execution  */
 12 int main ( void )
 13 {
 14 
 15		 int *array = NULL;
 16		 unsigned int size = 0;
 17		 int num, opt, pos;
 18		  
 19		 /* START DO STATEMENT */
 20		 do {  opt = printMenu();
 21				 
 22				 /* begin switch statement */
 23				 switch (opt) {
 24				   /* The user selected to add an intenger */
 25				   case 1:  /* Prompts for the intenger and reads it */
 26							printf("\nPlease enter the intenger you wish to add to the array\n");
 27							scanf("%d",&num);
 28 
 29							/*  Add number to the array */
 30							/* Checks if the operation had success */
 31							if ((array = addInt(array, num, size)) != NULL )
 32							size++;
 33							else 
 34							  printf("Can't add %d to array",num);
 35 
 36							printArray(array, size);
 37 
 38							break;
 39  
 40 
 41 
 42		 }
 43		 }  /* END DO */
 44		 while ( opt != 0 );
 45 
 46 
 47 
 48 return 0;
 49 }
 50 
 51 /* function to print the menu */
 52 int printMenu (void)
 53 {
 54 int opt;
 55 
 56 printf("Enter 1 to add an intenger\n");
 57 printf("Enter 2 to delete an intenger\n");
 58 printf("Enter 3 Print Array\n");
 59 printf("Enter 0 - Exit\n");
 60 scanf("%d",&opt);
 61 return opt;
 62 }
 63 
 64 /* function to print the array */
 65 void printArray ( int array [ ], int size )
 66 {
 67 
 68 int i;
 69 
 70 /* Check if array has number */
 71 if (size)
 72 {
 73   printf("Numbers in array:");
 74 
 75   for ( i = 0; i < size; i++ )
 76   printf("%d\n", array [ i ] );
 77 }
 78 else
 79   printf("Array is empty!");
 80 
 81   printf("\n\n");
 82 }
 83 
 84 /* function to add in to array */
 85 
 86 int addInt ( int array [ ], int num, int size )
 87 {
 88	int *temp = NULL;
 89	int i;
 90 
 91	/* Copies the original array to the new array */
 92 
 93	for ( i = 0; i < size - 1; i++ )
 94	  temp [ i ] = array [ i ];
 95 
 96	 /*  Inserts the new element on the top of the array */
 97 
 98	 temp [ size - 1 ] = num;
 99 
100	 /* returns the temp array */
101 
102	 return temp;
103 }




warning: assignment makes pointer from integer without a cast
.c:17: warning: unused variable ‚pos‚
.c: In function ‚addInt‚:
.c:102: warning: return makes integer from pointer without a cast

This is the first time I have encountered this. The program fails to a segmentation fault. I am trying to add number to an array, delete intengers from the array, sort the array, print the array.

I have searched the internet for the warning and found multiple programs but none of them really explain how to fix this warning or what is going on to fix the warning. I will put it this way. I didn't understand what fixes this if I encountered it.

Thanks


My guess is, at the end of the function int addInt ( int array [ ], int num, int size ) you are returning temp. But you see, temp is a pointer, but you're trying to return an int. So you should probably change that to return *temp; to fix that. At least that's my guess.

Edit: Just read through the rest of your code and to actually fix it to work the way you want it to I believe you have to change the function name to int *addInt( int array [ ], int num, int size ).

Either way, that is the basic idea.

This post has been edited by Einherjar: 02 July 2008 - 03:57 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1