Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 107,420 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,158 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



assignment make pointer from intenger without a cast

 
Reply to this topicStart new topic

assignment make pointer from intenger without a cast, return makes pointer from intenger without a cast

BSmith4740
post 2 Jul, 2008 - 02:49 PM
Post #1


New D.I.C Head

*
Joined: 10 Jun, 2008
Posts: 2

CODE


#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
User is offlineProfile CardPM

Go to the top of the page


Einherjar
post 2 Jul, 2008 - 03:55 PM
Post #2


D.I.C Head

**
Joined: 10 Feb, 2008
Posts: 73


My Contributions


QUOTE(BSmith4740 @ 2 Jul, 2008 - 05:49 PM) *

CODE


#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: 2 Jul, 2008 - 03:57 PM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 8/28/08 08:03PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month