casting problems warnings segmentation fault.

This is a discussion on casting problems warnings segmentation fault. within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> 2 #include <stdlib.h> 3 /* function prototypes */ 4 int printMenu(); 5 void printArray ( int [ ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    93

    casting problems warnings segmentation fault.

    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  
     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
    17: warning: unused variable ‚pos‚
    c: In function ‚addInt‚:
    102: warning: return makes integer from pointer without a cast
    I am having casting problems. I think I am returning an intenger to my add function and it expects an intenger. Any help is greatly appreciated.

    Thanks

  2. #2
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    The first warning is on line 31

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    You haven't learned a thing since that other topic, have you?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I see only NULL pointers, not a single memory allocation, lots of array accesses... What do you expect this all to do?

    As to warning on line 102:
    Code:
    int addInt ( int array [ ], int num, int size )
    {
        int *temp = NULL;
    ...
        return temp;
    The compiler is right. You are returning temp which is a pointer to integer, not integer.

    And could you stop posting code with line-numbers? People can't paste your code and compile it as is, if they should want to do that. Use other means such as colours / comments to mark lines that the compiler is complaining about.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    I didn't make temp an intenger of arrays on line 94?

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    I am trying to access an array add intengers to it, delete whatever the user wants to delete from the array and sort it.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    However, I can't do all of this in main. You have to use functions to write the program. For instance I can't declare.

    int a [ 20 ];

    The program has to print only what the user adds and stop when the user wants to stop. I tried declaring SIZE a global variable before main and between the function prototypes and incrementing the size.

    For instance,
    int size = 1;

    int main ()

    int a [ size ]

    Then incrementing the size as you go through and copy the array but that wasn't working either.

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    change the function prototype to

    Code:
    int *addInt ( int [ ], int, int);
    Warning go away but it does print. It just says segmentation fault.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    not print.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    I changed size to 1 at the beginning of main. I thought maybe my for loop was off for copying the array. I still get a segmentation fault.

    How do you resize an array in C if you can't declare the size starting off. Size isn't getting it done for some reason and I don't see why.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,423
    Gods!
    Stop posting 20 posts in a row.
    Use the edit button instead! It's there for a reason.

    Points:
    - Prototypes and definitions MUST match. No exceptions!
    - For dynamic arrays, use dynamic memory. That involves pointers and malloc and free. But you are far from ready to do this.

    Go back to basics, and re-read the other thread of yours.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    Pointers, malloc, and free are the only way to do this?


    Is this better? Eliza.

    Guess teach just expects us to pick this up on our own. lol Is malloc and free C or C++? I am assuming they are in:

    <stdlib.h>
    Last edited by BSmith4740; 07-02-2008 at 06:11 PM.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,423
    They are C, but understanding pointers is still beyond you. You can't even get functions right.
    You need to slow down and learn everything properly first.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Guess teach just expects us to pick this up on our own.
    Well if you have a teacher, then that means you probably have a C programming book. At least I've never taken a class without a textbook. Any C programming book will explain dynamic memory allocation. Regular arrays are declared a certain size, and don't expand. Try C++, Visual Basic, or some other language if you want expanding arrays and aren't comfortable with malloc() + realloc().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 06:32 AM
  2. Segmentation Fault
    By warfang in forum C++ Programming
    Replies: 9
    Last Post: 04-23-2007, 01:42 AM
  3. Re: Segmentation fault
    By turkish_van in forum C Programming
    Replies: 8
    Last Post: 01-20-2007, 04:50 PM
  4. Segmentation fault
    By BigAll in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2006, 05:25 AM
  5. strange segmentation fault error!
    By jayjay in forum Linux Programming
    Replies: 1
    Last Post: 10-20-2003, 03:25 PM

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