Thread: assign the pointer of a string to an integer

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    40

    assign the pointer of a string to an integer

    hi,
    i have this code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    int main(int argc, char **argv)
    {
      int *p;
      char *ddr = "abc";
      
      *p=ddr;
      
    }


    what i want is bassicaly to assign to the *p the pointer of the string so that

    i could do the following printf(" print string %s\n",*p);

    so i dont know how to do that.

    i thank you in advance for the help

  2. #2
    Registered User MilleniumFalcon's Avatar
    Join Date
    Feb 2014
    Posts
    33
    I can tell you right now that's impossible. Your compiler will give you an warning saying that the %s format expects a 'char *', but you gave it a value of type int. Then if you run your program, heaven forbid, your program will crash.

    If you really want to see how to convert a character pointer to an integer pointer, here is a working example:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main( )
    {
        int * i = NULL;
        char * secret = "This is a string literal!";
    
    
        i = ( int * )secret;
        printf( "%d\n", *i );
    
    
        return 0;
    }
    Last edited by MilleniumFalcon; 03-22-2014 at 04:22 PM.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    40
    i am compiling with the following options: gcc -g -Wall -ansi -pedantic testes.c -o tt

  4. #4
    Registered User MilleniumFalcon's Avatar
    Join Date
    Feb 2014
    Posts
    33
    Quote Originally Posted by tindala View Post
    i am compiling with the following options: gcc -g -Wall -ansi -pedantic testes.c -o tt
    It doesn't matter, it will still crash because you tried to print an integer like a string, which just doesn't work. If you're wanting to do something different, ask.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Why are you trying to store a char * in an int, anyway?

    I mean, it's not impossible to cast an address (which is what a pointer is, anyway) into an integer, but in general you have no guarantee that sizeof(int) is greater or equal to sizeof(char *), so you might overflow.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    what i want is bassicaly to assign to the *p the pointer of the string so that

    i could do the following printf(" print string %s\n",*p);
    If you want to see what is in the pointer, use
    Code:
    char *ddr = "abc";
    
    printf("Address is: %p", add);
    Fact - Beethoven wrote his first symphony in C

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by tindala View Post
    i could do the following printf(" print string %s\n",*p);
    What output do you expect to get when you do that?

    Note: as others have said, what you've described is a REALLY bad idea.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 07-14-2012, 09:26 AM
  2. Replies: 2
    Last Post: 04-30-2012, 08:05 PM
  3. assign a string to char pointer
    By seaking1 in forum C Programming
    Replies: 8
    Last Post: 12-08-2009, 02:54 PM
  4. Howto assign an address to an integer variable
    By daYz in forum C Programming
    Replies: 18
    Last Post: 02-24-2008, 10:19 AM
  5. Replies: 12
    Last Post: 10-14-2003, 10:17 AM

Tags for this Thread