Hi guys! I just wanna ask on how to access the first character in a string?
danswater
0
Newbie Poster
Recommended Answers
Jump to PostJust refer to it as mystr[0].
Jump to PostI wrote up this simple test program:
#include <stdio.h> int main() { char number[12]; printf("Enter a number: "); fgets(number, 12, stdin); printf("The first digit is: %c\n", number[0]); return 0; }
and it works for me.
…
Note that if you declare the char array with a pointer, like:
Jump to PostDid you want to access the first integer in a string?
or the first character in an integer? :-)How about the first integer in an array of integers?
int myarray[10] = {1,2,3,4,5,6,7,8,9,10}; printf("%d\n", myarray[0]); printf("%d\n", *myarray); printf("%d\n", myarray[5]); int *p; p = myarray; printf("%d\n", *p++); …
All 11 Replies
ryuurei
0
Newbie Poster
danswater
0
Newbie Poster
ryuurei
0
Newbie Poster
danswater
0
Newbie Poster
UncleLeroy
49
Junior Poster in Training
danswater
0
Newbie Poster
ryuurei
0
Newbie Poster
westony
0
Light Poster
danswater
0
Newbie Poster
Adak
419
Nearly a Posting Virtuoso
danswater
0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.