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.
}