Assinging value to *pointer

This is a discussion on Assinging value to *pointer within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to write a program to convert Hex to Bin. I found a useful piece of code ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    26

    Assinging value to *pointer

    Hi,

    I am trying to write a program to convert Hex to Bin. I found a useful piece of code on these forums to do just that except I have one problem. I want the user to enter a hex value, not have it defined:

    Code:
    int funcHextoBin()
    {
    char *userhexd;
    //char *userhexd = "92C3";
    char hex[] = "0123456789ABCDEF";
    char *bin[] = { "0000", "0001", "0010", "0011", "0100", "0101",
                      "0110", "0111", "1000", "1001", "1010", "1011",
                      "1100", "1101", "1110", "1111" };
    
    printf("Hex to Bin!\n");
    printf("Enter a Hexadecimal: \n");
    scanf("%s", userhex);
    //printf("You entered: %s\n", userhex);
    
    *userhexd = &userhex;
    
    while(*userhexd)
      {
        printf("%s", bin[strchr(hex, *userhexd)-hex]);
        userhexd++;
      }
      putchar('\n');
    
    system("PAUSE");
    return 0;
    }
    As you can see the original code had *userhexd with a predefiend value. All I want to change is so that the user enters a vlue. However right now the progrma crashes with a warning "assingment makes integer from pointer without a cast" I believe the problem is "*userhexd = &userhex;" Is there any special way I can assign the user's value to *userhexd?

    Thanks.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    To get userhexd into the function, pass it as a parameter. Obtain your string in the calling routine and pass this string to the function. Other than that, try to leave the function as you found it.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    26
    Can you please give an example of how to do that? I am relly new to C programming.

    Many thanks

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    void funcHextoBin ( const char *userhexd )
    {
       /* stuff */
    }
    Last edited by Dave_Sinkula; 03-06-2005 at 07:55 PM. Reason: It's late, I may be easily amused at the moment, but "Assinging...", as in, "I'm [i]a singing[/i] in the rain...". :)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

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