duncanb7
asked on
Tutorial: char, pointer in C
Dear Expert,
Recently , I am studing PHP explode() fuction convert C
at http://www.it.uu.se/katalog/larme597/explode
in which the example of explode() is working fine and but I
can not follow how it works.
Since I am not familar with C so I get the following simple basic questions.
For example, my code testing in C
#include <stdio.h>
int main (){
char h='a';
printf("=%c= =%d= =%d=\n",h,h,&h,);
// it will output "=a= =97= =1387254251="
//My question &h is address of char, h, but what is number of 97 stand for ?
char *g ="bc";
printf("=%c= =%c= =%d= =%d= \n",*g,*(g+1),g,&g);
//it will output "=b= =c= =4196522= =138725240="
//My question &g is address of pointer to g ?
//My question what is 4196522 stand for ?
//Why *(g+1) can get the second character of ("bc") ? whether g is address of the char
// or &g is address of char ?
}
I'm using gcc on 64 bit CentOS Linux
Please advise
Duncan
Recently , I am studing PHP explode() fuction convert C
at http://www.it.uu.se/katalog/larme597/explode
in which the example of explode() is working fine and but I
can not follow how it works.
Since I am not familar with C so I get the following simple basic questions.
For example, my code testing in C
#include <stdio.h>
int main (){
char h='a';
printf("=%c= =%d= =%d=\n",h,h,&h,);
// it will output "=a= =97= =1387254251="
//My question &h is address of char, h, but what is number of 97 stand for ?
char *g ="bc";
printf("=%c= =%c= =%d= =%d= \n",*g,*(g+1),g,&g);
//it will output "=b= =c= =4196522= =138725240="
//My question &g is address of pointer to g ?
//My question what is 4196522 stand for ?
//Why *(g+1) can get the second character of ("bc") ? whether g is address of the char
// or &g is address of char ?
}
I'm using gcc on 64 bit CentOS Linux
Please advise
Duncan
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
it is good
I can let the g char pointe into g[0] or g[1], sometimes, I don't know why it can
go into array.
Anyway let me try it before closing this thread.
I will open new thread for the example link, please look into it
http://www.it.uu.se/katalog/larme597/explode
Duncan
I can let the g char pointe into g[0] or g[1], sometimes, I don't know why it can
go into array.
Anyway let me try it before closing this thread.
I will open new thread for the example link, please look into it
http://www.it.uu.se/katalog/larme597/explode
Duncan
I did look at the example. It is taking a string and converting it into an array of strings. Or in this case, a pointer to character and converting it to an array of pointers to characters. Is something in that example not working for you?
ASKER
I can run the example sucessfully but I don't understand how it works for every code step
so I will made new thread for it instead of this thread
Anyway, I will do the array first as your suggestion and close this thread
Duncan
so I will made new thread for it instead of this thread
Anyway, I will do the array first as your suggestion and close this thread
Duncan
ASKER
I tried char *g="bc";
printf("%c\n", g[0]) will echo out "b";
so it is not need for declaration as array such as
char *g[]="bc"; Why no need ?
Duncan
printf("%c\n", g[0]) will echo out "b";
so it is not need for declaration as array such as
char *g[]="bc"; Why no need ?
Duncan
ASKER
Thanks for you reply
And you answer me the final questioon
and check my new thread of explode.c
And you answer me the final questioon
and check my new thread of explode.c
The reason you do not have to declare g as an array before referencing g[0] is because in C (or at least, in most implementations of C I've ever used), C automatically knows how to dereference char * variables as arrays of character, its kind of a built in feature.
So char* g = "bc"
printf("%c %c %c %c", *g, *(g+1), g[0], g[1]);
prints: b c b c
a g[1] translates to *(g+1) when dealing with pointer to character (pointers to bytes, essentially).
and g[x] translates to *(g+x) where x is an offset in bytes from the original starter position of the character pointer.
Note that if you wrote this:
char *g = "ab"
printf("%c", g[2]);
or
printf("%c", *(g+2);
Depending on the compiler you may get an error or may get a run time error because you are attempting to reference memory outside the boundary for char *g.
And if it does not error, it could produce unpredictable results. Whatever happens to be stored in the memory location 2 bytes after *g will be converted to char and printed.
On most OS if this causes you to attempt to read a part of memory not allocated to your program, you will get a memory access violation error.
So char* g = "bc"
printf("%c %c %c %c", *g, *(g+1), g[0], g[1]);
prints: b c b c
a g[1] translates to *(g+1) when dealing with pointer to character (pointers to bytes, essentially).
and g[x] translates to *(g+x) where x is an offset in bytes from the original starter position of the character pointer.
Note that if you wrote this:
char *g = "ab"
printf("%c", g[2]);
or
printf("%c", *(g+2);
Depending on the compiler you may get an error or may get a run time error because you are attempting to reference memory outside the boundary for char *g.
And if it does not error, it could produce unpredictable results. Whatever happens to be stored in the memory location 2 bytes after *g will be converted to char and printed.
On most OS if this causes you to attempt to read a part of memory not allocated to your program, you will get a memory access violation error.
ASKER
so *(g+1) can locate the second char of ("bc"), Right ?
I search it in goole, the example link is the best to
do explode() in C.
Those ***arr or arr[] or pointer address arithemtic or pointer of potiner made me
to lose track to the code.
So now I try to get it detail to printf out every step as debugger.
C is much faster than PHP in speed as ASM lanauage (if no doing DOM access) and can
handle a lot of memory allocation and data structure .
But C is not easy to be remembered that I used it before in long long time ago
and C sytnax is easy to forget