Thread: Can not convert int to int *

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    1

    Can not convert int to int *

    Code:
    #include "stdafx.h"
    # include "stdio.h"
    #include "conio.h"
    void swap(int *x,int *y);
    
    void main()
    {
    int a=10,b=20;
    swap(a,b);
    printf("value of a=%d and b=%d");
    getch();
    }
    
    void swap(int *x,int *y)
    
    {
      if(x!=y)
         {
          *x ^= *y;
             *y ^= *x;
             *x ^= *y;
    
         }
    }
    // I'm getting .. cann't convert int to int * ...

    can anybody tell me why so . and how to solve it
    regards.

    hoping for quick and positive response.

  2. #2
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Cool

    Quote Originally Posted by vishroxx View Post
    Code:
    #include "stdafx.h"
    # include "stdio.h"
    #include "conio.h"
    void swap(int *x,int *y);
    
    void main()
    {
    int a=10,b=20;
    swap(a,b);
    printf("value of a=%d and b=%d");
    getch();
    }
    
    void swap(int *x,int *y)
    
    {
      if(x!=y)
         {
          *x ^= *y;
             *y ^= *x;
             *x ^= *y;
    
         }
    }
    // I'm getting .. cann't convert int to int * ...

    can anybody tell me why so . and how to solve it
    regards.

    hoping for quick and positive response.

    Edit: The correct answer already provided below.
    Last edited by Char*Pntr; 09-06-2010 at 01:33 PM. Reason: comment on pointers

  3. #3
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    Code:
    int main()
    {
    int a=10,b=20;
    swap(&a,&b);
    printf("value of a=%d and b=%d",a,b);
    getch();
    return 0;
    }
    Swap function needs two pointers (memory addresses) and you're passing raw integer values instead of their memory addresses by using & in front of them.

    //Too Late for me
    Last edited by ch4; 09-06-2010 at 01:39 PM.

  4. #4
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    let's see... how many things are wrong here?

    You are trying to return an int value in your function prototype, yet you declared it
    to return "void".

    When you call your function, you wont capture anything, even if it was written
    correctly.

    Also you dont need to use pointers in your function.
    [STRIKETHROUGH]
    I formally refute these statements.

    -There are no returns anywhere, hence the declarations with void
    -The "swap" function swaps two variables in-place. There's no need to return something.
    -Pointers are necessary to swap variables like that (that is, in a function)
    [/STRIKETHROUGH]

    EDIT: Nevermind, it's gone now.

    With that said,
    1) void main() is evil. Do not use it. Please. See ch4's post for fixes.
    2) Take a look at this. Read it twice, maybe three times, let it really sink in.
    3) You're missing arguments for the printf function. Though it would have been easy to catch once you fixed the call to swap().
    Code:
    printf("value of a=%d and b=%d", a, b);
    EDIT: Holy carpal, these edits make it hard to keep track of what's going on!
    Last edited by bernt; 09-06-2010 at 01:46 PM.
    Consider this post signed

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by vishroxx View Post
    Code:
    void swap(int *x,int *y);
    
    ...
    
    void swap(int *x,int *y)
    
    {
      if(x!=y)
         {
          *x ^= *y;
             *y ^= *x;
             *x ^= *y;
    
         }
    }
    If you are using a good compiler, you are not gaining anything with that hacky xor swap. See here and here for more details.

  6. #6
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    Code:
    #include "stdafx.h"
    # include "stdio.h"
    #include "conio.h"
    void swap(int *x,int *y);
    Quotes are used to include custom libraries.
    For the default use <>

    Code:
    #include <stdafx.h>
    # include <stdio.h>
    #include <conio.h>
    void swap(int *x,int *y);

  7. #7
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    Quote Originally Posted by bernt View Post
    EDIT: Holy carpal, these edits make it hard to keep track of what's going on!
    Like goto

  8. #8
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    [QUOTE=bernt;968747][STRIKETHROUGH]
    I formally refute these statements.

    -There are no returns anywhere, hence the declarations with void
    -The "swap" function swaps two variables in-place. There's no need to return something.
    -Pointers are necessary to swap variables like that (that is, in a function)
    [/STRIKETHROUGH]

    EDIT: Nevermind, it's gone now.

    I already deleted my suggestion.... like seconds after I posted it... surprised
    that it hung on for so long.

  9. #9
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Smile

    Quote Originally Posted by ch4 View Post
    Like goto
    I took my first C class back in '92... the instructor who claimed he knew
    Dennis Ritchie, told us that if we ever use a "goto" we would get
    an "F" for the assignment.

    He said the only reason the "goto" statement was included was because the ANSI C committee required it. Probably Ritchie was not too happy with that decision. But since I did not personally know him, maybe he used "goto's" for debugging or whatever. I've never used it, and I don't think I ever will.

    That's pretty much all that remembered from the class since I haven't used
    C since then.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > He said the only reason the "goto" statement was included was because the ANSI C committee required it.
    This is a load of BS.
    goto was part of the language long before ANSI got anywhere near it, It wasn't their decision to add or remove it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by Salem View Post
    > He said the only reason the "goto" statement was included was because the ANSI C committee required it.
    This is a load of BS.
    goto was part of the language long before ANSI got anywhere near it, It wasn't their decision to add or remove it.
    I think I misquoted the statement that I heard over 18 years ago.... "ANSI C committee included the "goto" in the standard to make it backward compatible with existing C code?

    Anyway, I'm off topic.

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Quote Originally Posted by Char*Pntr
    the instructor ... told us that if we ever use a "goto" we would get
    an "F" for the assignment.
    This is probably good advice for a beginning class. I suspect a lot of students would have been exposed to BASIC or something similar, where the use of goto was normal, and would need to be taught how to think in another way.

    He said the only reason the "goto" statement was included was because the ANSI C committee required it.
    This can't be true. The Unix V4 kernel, dating from around 1973, has at least 90 cases of goto being used; both Ken Thompson and Dennis Ritchie used goto, if directory names are to be believed. The 1975 V6 system (kernel + userland) had at least 800 uses of goto. ANSI didn't even start looking at C until the early 1980s.

    While goto can be abused, there are some legitimate uses for it. Common examples include breaking from inside nested loops, and for cleanup on error.

    Edit: Late reply. Sorry.
    Last edited by cas; 09-06-2010 at 04:00 PM. Reason: I didn't hit reload, apparently...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

Tags for this Thread