473,670 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6732
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.hel sinki.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******@mkwah ler.net> wrote:
Matthew Jakeman <ma************ @yahoo.co.uk> wrote in message
news:2003073021 0038.2ba49ef8.m a************@y ahoo.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:2003073021 2931.42e9d2a1.m a************@y ahoo.co.uk...
On Mon, 28 Jul 2003 12:18:06 -0700
"Mike Wahler" <mk******@mkwah ler.net> wrote:
Matthew Jakeman <ma************ @yahoo.co.uk> wrote in message
news:2003073021 0038.2ba49ef8.m a************@y ahoo.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:2003073022 1954.41c59531.m a************@y ahoo.co.uk...
On Mon, 28 Jul 2003 13:00:29 -0700
"Mike Wahler" <mk******@mkwah ler.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******@mkwah ler.net> wrote:

Matthew Jakeman <ma************ @yahoo.co.uk> wrote in message
news:2003073021 2931.42e9d2a1.m a************@y ahoo.co.uk...
> On Mon, 28 Jul 2003 12:18:06 -0700
> "Mike Wahler" <mk******@mkwah ler.net> wrote:
>
> > Matthew Jakeman <ma************ @yahoo.co.uk> wrote in message
> > news:2003073021 0038.2ba49ef8.m a************@y ahoo.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******@mkwah ler.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******** **********@news fep1-win.server.ntli .net...
<snip>

First of all Mike has been helping people on this newsgroup (with solid
answers) since I can remember(throug h 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.hel sinki.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.hel sinki.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

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

Similar topics

6
10775
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
4775
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
10623
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 namespace std; : : vector<string>* getTyphoon()
13
11011
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 below would work... string gsub(const string & sData, const string & sFrom,
16
16414
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
17410
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 * is 4 bytes, should the following yield 4, 5, of something else? (And, if something else, what determines the result?) char x = "abcd"; printf( "%d\n", sizeof( x ) ); -Don
7
4353
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 type of "const char *". Right? But why does the compiler I am using allow s to be modified, instead of generating compile error?
4
8807
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 a * occurs in the string). This split function should allocate a 2D array of chars and put the split results in different rows. The listing below shows how I started to work on this. To keep the program simple and help focus the program the...
6
22742
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? // Remove (first occurence of) sub from src void func(char *src, char *sub) { char *p;
3
5090
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 (Java). It stores character strings, and resizes itself to accommodate new strings when needed. Interface: ----------
0
8471
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8388
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8817
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8663
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7423
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5687
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4396
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1799
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.