|
-
August 5th, 2003, 10:49 PM
#3
Pointers are not as difficult as people think.
int a=100;
with that you declare an integer with a value of 100. To have its address you use the & operator
printf( "the value: %d -- the address: %d \n", a, &a);
If you want to declare a pointer you must use the * operator
int *int_ptr;
All you have to know is if you want the value you must use *int_ptr and if you want the address you must use int_ptr (this is a short of &(*int_ptr) ).
So if you want pointing int_ptr to the address of a
int_ptr = &a; /* address of int_ptr == address of a */
if you want the value
printf( "value of a: %d -- value of int_ptr: %d\n", a, *int_ptr);
This is exactly the same for the string so when you write
char *array = "Hello World";
sprintf("a string %s", array);
array point to the address of the string "Hello World" so the print function write all the char beginning at the address array and end when the char \0 (end of string) is found.
a string is an array of char
char MyString[] = "Hello World";
the address of the array start at the address of the first char: &MyString[0] but this is the same as writing MyString so
char *ptr_string1;
char *ptr_string2;
ptr_string1 = &MyString[0];
ptr_string2 = MyString;
printf( "string1: %s -- string2: %d\n", ptr_string1, ptr_string2);
Hope this will help people
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|