473,387 Members | 1,638 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

String to char

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 ?

Nov 13 '05 #1
13 6703
Matthew Jakeman <ma************@yahoo.co.uk> scribbled the following:
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 (pa*****@cc.helsinki.fi) ---------------------------\
| 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
Nov 13 '05 #2
On Mon, 28 Jul 2003 12:18:06 -0700
"Mike Wahler" <mk******@mkwahler.net> wrote:
Matthew Jakeman <ma************@yahoo.co.uk> wrote in message
news:20030730210038.2ba49ef8.ma************@yahoo. co.uk...
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.

Nov 13 '05 #3

Matthew Jakeman <ma************@yahoo.co.uk> wrote in message
news:20030730212931.42e9d2a1.ma************@yahoo. co.uk...
On Mon, 28 Jul 2003 12:18:06 -0700
"Mike Wahler" <mk******@mkwahler.net> wrote:
Matthew Jakeman <ma************@yahoo.co.uk> wrote in message
news:20030730210038.2ba49ef8.ma************@yahoo. co.uk...
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

Nov 13 '05 #4

"Matthew Jakeman" <ma************@yahoo.co.uk> wrote in message
news:20030730221954.41c59531.ma************@yahoo. co.uk...
On Mon, 28 Jul 2003 13:00:29 -0700
"Mike Wahler" <mk******@mkwahler.net> wrote:
<snip>

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.. I didn't, i asked for help,


Help given here is intended to supplement, not replace,
the more traditional learning materials, such as books.


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.

--
Andy Zhang
Nov 13 '05 #5
bd
On Wed, 30 Jul 2003 22:19:54 +0100, Matthew Jakeman wrote:
On Mon, 28 Jul 2003 13:00:29 -0700
"Mike Wahler" <mk******@mkwahler.net> wrote:

Matthew Jakeman <ma************@yahoo.co.uk> wrote in message
news:20030730212931.42e9d2a1.ma************@yahoo. co.uk...
> On Mon, 28 Jul 2003 12:18:06 -0700
> "Mike Wahler" <mk******@mkwahler.net> wrote:
>
> > Matthew Jakeman <ma************@yahoo.co.uk> wrote in message
> > news:20030730210038.2ba49ef8.ma************@yahoo. co.uk...
> > > 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.

--
Freenet distribution not available
Having a wonderful wine, wish you were beer.

Nov 13 '05 #6

On Wed, 30 Jul 2003, Andy Zhang wrote:

"Matthew Jakeman" <ma************@yahoo.co.uk> wrote in message
"Mike Wahler" <mk******@mkwahler.net> wrote:
>
> 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..
Help given here is intended to supplement, not replace,
the more traditional learning materials, such as books.


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.


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

Nov 13 '05 #7
"Matthew Jakeman" <ma************@yahoo.co.uk> wrote in message
news:E3******************@newsfep1-win.server.ntli.net...
<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.
Nov 13 '05 #8

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:bg**********@oravannahka.helsinki.fi...
Matthew Jakeman <ma************@yahoo.co.uk> scribbled the following:
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 (pa*****@cc.helsinki.fi) ---------------------------\
| 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
Nov 13 '05 #9


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
Nov 13 '05 #10

"Matthew Jakeman" <ma************@yahoo.co.uk> wrote in

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[i] != 0; i++)
if(string[i] != tolower(string[i]))
/* string[i] is a capital, we have the start of the name */

Nov 13 '05 #11
Matthew Jakeman <ma************@yahoo.co.uk> wrote in message
news:20030730221954.41c59531.ma************@yahoo. co.uk...

[snip]
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.


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

Nov 13 '05 #12
In <20************************************@yahoo.co.u k> Matthew Jakeman <ma************@yahoo.co.uk> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #13
Matthew Jakeman wrote:
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

Nov 13 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Gaurav | last post by:
Hello, I am using visual c++ 6 and i am having problems with string to work. ******** Here is the program project.cpp********* #include <iostream.h> #include <string> #include "stdafx.h"
2
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
13
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with another string. I would have thought that the code...
16
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: CxImage(byte* data, DWORD size) Thx in advance
16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
6
by: becte | last post by:
I am little bit confused Is this a legal way of removing a substring from a string? What about the second alternative using strcpy, is it ok even though the source and dest. strings overlap? ...
3
by: jacob navia | last post by:
Abstract: Continuing the discussion about abstract data types, in this discussion group, a string collection data type is presented, patterned after the collection in C# and similar languages...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.