Solved

Tutorial: char, pointer in C

Posted on 2013-11-05
10
768 Views
Last Modified: 2013-11-05
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
0
Comment
Question by:duncanb7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 8

Assisted Solution

by:virtuadept
virtuadept earned 333 total points
ID: 39625052
97 is the decimal representation of ascii character a (lower case A).

&g is the address of the pointer to g.

4196522 is the address that *g points to.

*(g + 1) means add 1 to the address of g and return its value, which gives you c because the next byte contains a "c" character.

I hate pointer arrithmetic and not every language supports it.
0
 
LVL 16

Assisted Solution

by:HooKooDooKu
HooKooDooKu earned 167 total points
ID: 39625062
A char is really just an integer variable with a range of -128 to 127.
The line
    printf("=%c=  =%d=  =%d=\n",h,h,&h,);
is saying to print the variable value of h as a character, then as an integer, then finally the address of the variable as an integer.

In the case of
  char *g ="bc";
the text "bc" is a hard-coded string that will exist somewhere in the data of the program, and g is now a pointer to that string.  Technically, g is a pointer to the 1st character of the string.
The line
  printf("=%c=   =%c=   =%d=  =%d= \n",*g,*(g+1),g,&g);
is saying to print what g points to as a character (so you get the 1st character of the string)
and print what is 1 byte past what g points to as a character (so you get the 2nd character of the string)
It then says to print the value of g (a pointer) as an integer, and then print the address of g (a pointer to a pointer) as an integer.
0
 
LVL 13

Author Comment

by:duncanb7
ID: 39625086
g and h in printf with %d have different interpretation since g is pointer to char,
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
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 8

Accepted Solution

by:
virtuadept earned 333 total points
ID: 39625102
correct, g was defined as a char* which is a pointer to a character. h was defined as a character. so g is just an address that points at a the first byte of memory that will contain the character values "bc". That is why you can do *(g+1) to get the 2nd character.

In C that is not typically how programs do string manipulation. Usually a char* will be terminated with a null (\0) and that is how the C string functions know the end of the contiguous block of bytes representing characters that are the "string".

I could be remembering wrong but you might could also reference the "c" in "bc" with g[1] treating the char* as an array of characters. g[0] would be "b". That may depend on the C implementation.
0
 
LVL 13

Author Comment

by:duncanb7
ID: 39625157
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
0
 
LVL 8

Expert Comment

by:virtuadept
ID: 39625165
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?
0
 
LVL 13

Author Comment

by:duncanb7
ID: 39625176
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
0
 
LVL 13

Author Comment

by:duncanb7
ID: 39625209
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
0
 
LVL 13

Author Closing Comment

by:duncanb7
ID: 39625215
Thanks for you reply

And you answer me the final questioon

and check my new thread of explode.c
0
 
LVL 8

Expert Comment

by:virtuadept
ID: 39625398
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.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question