String to char

M

Mike Wahler

Matthew Jakeman said:
Matthew Jakeman said:
If i have a pointer, char *.

Note that an object of type 'char*' (pointer to char),
is neither a character nor a string. It can represent
the address of an indivudual character (type 'char')
object. It cannot represent a character or a string.

That i have checked is only 1 character long,

It's very unlikely (but not impossible) that a pointer
on your system is only one character in size. And
again, note that this type can only contain an *address*
of a memory location, not a character value.

is just casting it to a char using (char)

Casting a char* to a char gives undefined (or is it
implementation-defined?) behavior.
the best way or is there another way ?

Best way to do what?

If you have a pointer of type 'char*' which contains the
address of a character object, or the address of the first
of an array of characters, you access the 'pointed to'
character with the dereference operator:

char some_stuff[] = "Hello";
char *p;
char c;

p = &some_stuff[1];
c = *p; /* The object 'c' now contains the character 'e' */

What C book(s) are you reading that don't explain
pointers?

-Mike

At what point in my message did i say i had been reading a C book / books
that explain pointers ?

You didn't. But imo you're only wasting your time
trying to learn C without textbooks. You can't learn
it 'piecemeal' by posting questions to usenet.

I didn't, i asked for help,

Help given here is intended to supplement, not replace,
the more traditional learning materials, such as books.
thanks for the paragraphs that have managed to make a simple question,

Your question wasn't only 'simple', but it was quite vague,
and indicated misconceptions, which I attempted to rectify.
from someone who is obviously dwarfed by your mass of intelligence,

Obnoxious sarcasm won't get you very far here.
turn into something immensely more complex


I don't think any of my explanation of a simple topic
was complex at all. If you find it so, then you really
do need some books.
but the reply before yours was a lot less demeaning


Please point out where in my reply I used any
'demeaning' text. All I did was state facts.
If you're upset with those facts, take it up
with them.
and a hell of a lot more helpful.

It might be, it might not. Joona had to guess at
what you really wanted. He also apparently chose
to overlook the parts of your post that indicate
your misconceptions about C.

-Mike
 
M

Mike Wahler

[snip]
Maybe misconceptions maybe i am just crap at expressing myself

Perhaps. But you surely don't seem very good at
interacting with people from whom you seek help.
gramatically,

Your grammar is not the issue of this thread.
there might be a difference,

Yes, there is indeed a difference between having a
misconception about something and being able to
express oneself clearly.
and how do u know he guessed,

Because you gave insufficient information for a
conclusive answer.
maybe he could understand it

He tried to, while making assumptions.
i dont know but he dechpihered it somehow,

Apparently he made a correct guess. It happens sometimes.
oh and please feel free to correct my spelling as well next time!

Your spelling looks mostly OK, it's your grammar,
punctuation, and capitalization that are atrocious. :)

-Mike
 
J

Joona I Palaste

Matthew Jakeman said:
If i have a pointer, char *. That i have checked is only 1 character long, is just casting it to a char using (char) the best way or is there another way ?

Casting it to (char) is definitely the *WRONG* way. Use the * operator
for what it is meant for.

char *p;
/* ... */
char c;
c = *p;

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"You can pick your friends, you can pick your nose, but you can't pick your
relatives."
- MAD Magazine
 
M

Matthew Jakeman

If i have a pointer, char *. That i have checked is only 1 character long, is just casting it to a char using (char) the best way or is there another way ?
 
M

Matthew Jakeman

Matthew Jakeman said:
If i have a pointer, char *.

Note that an object of type 'char*' (pointer to char),
is neither a character nor a string. It can represent
the address of an indivudual character (type 'char')
object. It cannot represent a character or a string.

That i have checked is only 1 character long,

It's very unlikely (but not impossible) that a pointer
on your system is only one character in size. And
again, note that this type can only contain an *address*
of a memory location, not a character value.

is just casting it to a char using (char)

Casting a char* to a char gives undefined (or is it
implementation-defined?) behavior.
the best way or is there another way ?

Best way to do what?

If you have a pointer of type 'char*' which contains the
address of a character object, or the address of the first
of an array of characters, you access the 'pointed to'
character with the dereference operator:

char some_stuff[] = "Hello";
char *p;
char c;

p = &some_stuff[1];
c = *p; /* The object 'c' now contains the character 'e' */

What C book(s) are you reading that don't explain
pointers?

-Mike

At what point in my message did i say i had been reading a C book / books that explain pointers ? I didn't, i asked for help, thanks for the paragraphs that have managed to make a simple question, from someone who is obviously dwarfed by your mass of intelligence, turn into something immensely more complex but the reply before yours was a lot less demeaning and a hell of a lot more helpful.
 
A

Andy Zhang

Matthew Jakeman said:
On Mon, 28 Jul 2003 13:00:29 -0700


At what point in my last reply did i say i hadn't been using books ? See
being a pedantic bastard who makes wrong assumptions can be annoying to some
people..
AGAIN, i never said i hadn't been using books, u assumed that i believe.

Huh? But you just said you weren't using books. Now I'm just confused.
 
B

bd

Matthew Jakeman said:
On Mon, 28 Jul 2003 12:18:06 -0700

If i have a pointer, char *.

Note that an object of type 'char*' (pointer to char), is neither a
character nor a string. It can represent the address of an
indivudual character (type 'char') object. It cannot represent a
character or a string.


That i have checked is only 1 character long,

It's very unlikely (but not impossible) that a pointer on your
system is only one character in size. And again, note that this
type can only contain an *address* of a memory location, not a
character value.


is just casting it to a char using (char)

Casting a char* to a char gives undefined (or is it
implementation-defined?) behavior.

the best way or is there another way ?

Best way to do what?

If you have a pointer of type 'char*' which contains the address of
a character object, or the address of the first of an array of
characters, you access the 'pointed to' character with the
dereference operator:

char some_stuff[] = "Hello";
char *p;
char c;

p = &some_stuff[1];
c = *p; /* The object 'c' now contains the character 'e' */

What C book(s) are you reading that don't explain pointers?

-Mike





At what point in my message did i say i had been reading a C book /
books
that explain pointers ?

You didn't. But imo you're only wasting your time trying to learn C
without textbooks. You can't learn it 'piecemeal' by posting questions
to usenet.
At what point in my last reply did i say i hadn't been using books ? See
being a pedantic bastard who makes wrong assumptions can be annoying to
some people..

Any C book should explain pointers. If it dosen't, get a new one.
 
A

Arthur J. O'Dwyer

Huh? But you just said you weren't using books. Now I'm just confused.

Gee, haven't you ever read GEB? Just because the Tortoise says his
shell is green, and also that his shell is not green, you take a
perverse fascination in trying to make him admit a contradiction,
viz., that his shell is both green and not green! No wonder he's
annoyed!

;-)

-Arthur
 
G

Greg P.

<snip>

First of all Mike has been helping people on this newsgroup (with solid
answers) since I can remember(through all my different "handle" names that
I've used to post here), and from what I've seen in the past he meant
nothing "demeaning" by his reply. Many people take offense when their lack
of experience gets a light shined on it, including me sometimes. If you
noticed, he did not "flame" back.

The fact that he replied with more information than you had probably liked
is a good thing, as it is something you can learn by, rather than a single
answer that does not explain why what you are trying to do is invalid (from
char* to char). When posting on here please don't take informative replies
for granted.
 
A

Allan Bruce

long, is just casting it to a char using (char) the best way or is there
another way ?
Casting it to (char) is definitely the *WRONG* way. Use the * operator
for what it is meant for.

char *p;
/* ... */
char c;
c = *p;

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"You can pick your friends, you can pick your nose, but you can't pick your
relatives."
- MAD Magazine

If you just want the first character (or if you are sure the pointer only
poits to onw char) then you can simply use:

char *p;
// ...
printf("%c\n",p[0]); // or whatever you want to with the char

Allan
 
D

Default User

Matthew Jakeman wrote:

At what point in my last reply did i say i hadn't been using books ?


At what point did you decide that the way to get a newsgroup to help you
out was to act like a horse's ass? You're the one that doesn't
understand basic concepts, Mike (who's a good guy) took a lot time to
explain a good deal of relevant information to you. Instead of saying
thanks, you behave like a petulant child.

I suggest you get a major attitude adjustment immediately. Apologizing
would be a great start. Otherwise you'll end up in the collective bozo
files of the knowledgable group members, then you post your requests to
no one.




Brian Rodenborn
 
M

Malcolm

Matthew Jakeman said:
And that doen't mean that i have been using a book / books, unless my
grammar is wrong again, but i dont believe that it was stated and as it
was so essential for Mike to pick at every little thing that i had said i
thought i would do the same!
Computer programming attracts very literal-minded people. The reason is
obvious. If you say "this is a string, well really its a pointer to a
string, know what I mean?" a human may understand, but a computer hasn't a
clue.

You really need to crack pointers to get anywhere with C. A pointer is a
variable that holds an address. A char *holds the address of a character, an
int * holds the address of an integer etc.

Now C also allows you to store arrays. An array is a series of variables
which are continuous in memory. If we declare an array

int *array[6] = {1,2,3,4,5,6};

/* set ptr to first element of the array */
int *ptr = &array[0];

then *ptr = 1; *(ptr+1) = 2, *(ptr+2) = 3 etc.

we can also use [] notation.

ptr[0] = 1, ptr[1] = 2, ptr[2] = 3 etc.
Note that the variable ptr, which is a pointer, is almost equivalent to the
varaible "array".

A string is just an array of chars.

char *string = "my name is Fred".

Now only very seldom will you need to take the address of a single char.
Usually a char * will point to the first element of a string. Here "string"
is pointing to 'm'.

Casting "string" to a char is meaningless, because "string" holds the
address of 'm' in memory. What will happen on most machines is that the last
eight bits of the address will be converted to a character value, which is
of no use to anybody.

However

char ch = *string;

will set ch = 'm'.

Very frequently you will need to step through strings, by applying an idex
to pointer "string".

eg

for(i=0;string != 0; i++)
if(string != tolower(string))
/* string is a capital, we have the start of the name */
 
D

Dan Pop

In said:
If i have a pointer, char *. That i have checked is only 1 character
long, is just casting it to a char using (char) the best way or is
there another way ?

Ever considered reading a C book?

Dan
 
P

Peter Shaggy Haywood

Whiney bitch Matthew Jakeman was jivin' on Wed, 30 Jul 2003 22:19:54
+0100 in comp.lang.c.
Re: String to char's a bad trip! Dig it!
At what point in my last reply did i say i hadn't been using books ?
See being a pedantic bastard who makes wrong assumptions can be
annoying to some people..

You can't have it both ways. Either you have books or you don't. If
you do, then bloodywell read them, and stop being such a whiney bitch!
If you don't have books, then bloodywell get some, bloodywell read
them, and stop being such a whiney bitch!
AGAIN, i never said i hadn't been using books, u assumed that i believe.

See above.
The reply before yours somehow managed to decrypt my mystic vague
message to give me an answer in a few lines!!

Then it did so by accident. Your question was not clear. The
previous respondant had to guess what you were trying to say.
Sorry, but i thought that was exactly what your reply to my question was

Mike was trying to help you. If you go off half cocked every time
someone tries to help you, then you'll end up without help. Be polite,
patient and courteous, especially to those whose assistance you seek.
And stop being such a whiney bitch!
I didn't say it was complex, i said more complex and once again i have
some books, you are making an assumption from something i never said.

If it isn't complex, then what are you whining about?
Besides, you said, "immensely more complex." Well, either it's too
hard for you to understand or it isn't. You can't have it both ways.
Or was that just a hissy-fit exaggeration?
Maybe misconceptions maybe i am just crap at expressing myself
gramatically, there might be a difference, and how do u know he
guessed, maybe he could understand it i dont know but he dechpihered
it somehow, oh and please feel free to correct my spelling as well
next time!

Don't be such a baby! You have not been defamed by anyone but
yourself. The only person making you look like an idiot is you. Get
over yourself, and learn to conduct yourself in the proper manner.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
P

Peter Shaggy Haywood

Whiney bitch Matthew Jakeman was jivin' on Wed, 30 Jul 2003 20:40:42
+0100 in comp.lang.c.
Re: String to char's a bad trip! Dig it!
If you read it, i never actually say whether i have read a book or not.

Now who's being a pedantic bastard, hm, and a hypocrite to boot?

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
P

Philip Willoughby

Matthew said:
If you read it, i never actually say whether i have read a book or not.

At what point in my message did i say i had been reading a C book /
books that explain pointers ?

That doesn't mean i didnt read a book!

At what point in my last reply did i say i hadn't been using books ?

And that doen't mean that i have been using a book / books, unless my
grammar is wrong again, but i dont believe that it was stated and as it was
so essential for Mike to pick at every little thing that i had said i
thought i would do the same!

Can I just add a small comment to this thread -- rfc 1855 which you can
download from:

http://www.ietf.org/rfc/rfc1855.txt

Is a document you would benefit from reading...

An excerpt from section 3.1.1:

If you should find yourself in a disagreement with one person,
make your responses to each other via mail rather than continue to
send messages to the list or the group. If you are debating a
point on which the group might have some interest, you may
summarize for them later.

However, I think I speak for everyone here when I say that we have no
interest in whether you did or did not say you hadn't read a book.

<joke>
Maybe you should move this thread to alt.semantics.english?
</joke>

Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top