C pointer

This is a discussion on C pointer within the C Programming forums, part of the General Programming Boards category; what is the use of \x in C ? and can pointer show the memory address of char in C ...

  1. #1
    Unregistered
    Guest

    C pointer

    what is the use of \x in C ?

    and can pointer show the memory address of char in C ?

    such as

    #include <stdio.h>
    void main()
    {
    char *c;
    *c = 'a';
    printf("%c",c);
    }

    it has a error , Access violate .

    anyone know what is it ( Access violate ) ?

    does any ways can show the char memory address or can use a pointer in char ? thank you

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    you have to point your pointer at something before you use it
    Code:
    char ch, *c; 
         c = &ch;
         *c = 'a'; 
    
         printf("%c", c);
    Try using %x, it puts out hexidecimal, if you use with a pointer it might print the address, not sure though, can someone clarify that?
    Code:
    char ch, *c;
         c = &ch;
    
         printf("%x %x", ch, c);
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    what is the use of \x in C ?
    \x is an escape sequence used to represend character constants as hex values. For example, the hex value of '0' is 30 (I think), so
    '\x30'
    is the same as
    '0'

    Code:
    #include <stdio.h> 
    void main() // <- Please use int main.
    { 
     char *c;
     *c = 'a'; // Crashes here
     printf("%c",c);
    }
    c is defined as a memory address. You have not given any information on what memory address is represented by c, so it is not unlikely that c points to an invalid memory address. Technically, it is possible that c is pointing at the area of memory in which your program is loaded!

    The command to allocate some memory is malloc, and it would be used like this...
    Code:
    #include <stdio.h> 
    int main() 
    { 
     char *c;
     c = malloc (sizeof(char)); // Now c points to a valid memory location.
     *c = 'a'; 
     printf("%c",*c); 
    }
    So now *c = 'a' will not give you problems. However, if you want your printf starement to make sense, you need to tell it to print the memory that is being pointed to by c... that is, printf ("%c", *c); not to print the memory address itself.

    There are other neat things one can do with pointers...
    Code:
    #include <stdio.h> 
    int main() 
    { 
     char *c;
     char d;
     d = 'b'
     c = &d; // &d is the memory address of d.
     *c = 'a';
     printf("%c",d); // Viola, we have changed the value in d without
     //  actually using d.
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Oh, if you want to print a pointer, use...[code]printf ("%p", c);
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 04:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 08:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

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