C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-05-2009, 01:06 AM   #1
Registered User
 
Join Date: Mar 2008
Posts: 39
Post question about the array type char

There is code in C
Code:
int main(void)
{
    char *a = "Hello, World!";
    printf("%s\n",&a[1]);
    return 0;
}
I thought the output will be H but in fact it is not. Can someone explain it to me and how can modify my code to get letter H from that string.Thanks
thungmail is offline   Reply With Quote
Old 11-05-2009, 01:17 AM   #2
Registered User
 
Join Date: Oct 2009
Location: While(1)
Posts: 316
Because you printging the string which will start from base address + 1
in your case a + 1

if you will change you code to
Code:
printf("%c\n",a[0]);
you will get your answer then
RockyMarrone is offline   Reply With Quote
Old 11-05-2009, 01:37 AM   #3
Registered User
 
Join Date: Mar 2008
Posts: 39
thanks for your help but i still feel confused if i use
Code:
printf("%s\n",&a[1]);
it will give me
Code:
ello, World!
I dont get it and if we use & operator at here, why the result is like this.
thungmail is offline   Reply With Quote
Old 11-05-2009, 01:40 AM   #4
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,368
Quote:
Originally Posted by thungmail
Why we can not use & operator at here.
You want to print a character, so you should get the character at your desired index and print it as a character, not a string. The use of address-of in your example provides a pointer to the character at the index 1, which when you print as a string, prints the string starting from index 1.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 11-05-2009, 02:31 AM   #5
Registered User
 
Join Date: Mar 2008
Posts: 39
Thanks for all your answers.
For the problem above, according to laserlight
Quote:
Quote:
You want to print a character, so you should get the character at your desired index and print it as a character, not a string. The use of address-of in your example provides a pointer to the character at the index 1, which when you print as a string, prints the string starting from index 1.
I made a small change
Code:
printf("%s\n",*a[1]);
But still get the error.

This error still occurs when I try an example above.
If I have
Code:
char * str
and str is "ABCDEF"
if i have
Code:
1.for(;*(str+i) != '\0';i++)
2.{
3.    if(isupper(str[i]))              // Check if every char in str is capital
4.            do something
5.}
I dont understand at line 1 and 3. As i remember * operator will give you a content of an object which pointer is pointing to. So*(str+i) will give you content at ith position of str.I tried
Code:
isupper(*(str[i]))
but the compiler complains
Code:
 invalid type argument of `unary *'
I dont get it and also, why we have to implement such syntax in line 1

Last edited by thungmail; 11-05-2009 at 02:47 AM.
thungmail is offline   Reply With Quote
Old 11-05-2009, 02:34 AM   #6
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,368
Quote:
Originally Posted by thungmail
As i remember * operator will give you a content of an object which pointer is pointing to. So*(str+i) will give you content at ith position of str.
Correct, so the idea is to keep looping until you have found the first null character (since strings in C are null terminated). Of course, you could have also used str[i] != '\0' for the comparison.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 11-05-2009, 09:09 AM   #7
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
The index operator dereferences the pointer, so in effect a[1] is the same as *(a + 1), so no * is necessary.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 11-05-2009, 10:13 AM   #8
Registered User
 
Join Date: Mar 2008
Posts: 39
Thanks a lot it is clear now
There is another question I want to ask about the fgets() in C. Here is my code
Code:
#include <string.h>
#include <stdio.h>
1.int main (int argc, char *argv[]){
2.	char* input;
3.	char* arg;
4.	fgets ( input ,43, stdin ) ;
5.     arg = strtok(input," ");          
6.	printf("%s\n",arg);
7.}
After compiling and executing the code, I got the error
Code:
red 313 % input.out
input from the screen
Segmentation fault
I thought it should print
Code:
input
from
the
screen
but it did not. Can you explain it for me pls.Thanks
thungmail is offline   Reply With Quote
Old 11-05-2009, 10:41 AM   #9
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
SourceForge.net: Common mistakes and errors - cpwiki
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie needs help with code compudude86 C Programming 6 07-23-2006 08:54 PM
How to fix misaligned assignment statements in the source code? biggyK C++ Programming 28 07-16-2006 11:35 PM
Personal Program that is making me go wtf? Submeg C Programming 20 06-27-2006 12:13 AM
Please Help - Problem with Compilers toonlover C++ Programming 5 07-23-2005 10:03 AM
linked list inside array of structs- Syntax question rasmith1955 C Programming 14 02-28-2005 05:16 PM


All times are GMT -6. The time now is 03:40 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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