simple pointers question

R

Richard Heathfield

Pietro Cerutti said:

If temp2 points to an array of chars where the first is '3', the
second is '2' and the third is '\0', then the function atoi() from
stdlib.h may help.

samples = atoi(temp2);

Whilst that's fine provided temp2 does indeed point to "32", it is
nevertheless a bad idea, in general, to advocate atoi, because its
behaviour on overflow is undefined. It would be far better to use
strtol.
 
P

Pietro Cerutti

Richard said:
Pietro Cerutti said:



Whilst that's fine provided temp2 does indeed point to "32", it is
nevertheless a bad idea, in general, to advocate atoi, because its
behaviour on overflow is undefined. It would be far better to use
strtol.

Thanx for pointing it out.
 
K

Keith Thompson

Im sorry guys I was not clean and now I realize it.
I now see what you guys where talking about, and you are correct that
I was way off.


I have:

char *temp;
//Lets say that this is pointing to the word "Samples"

Ok. For example, you might have declared:

char *temp = "Samples";

or, better:

const char *temp = "Samples";
char *temp2
//Lets say that this is pointing to the number '32'

Um, let's say it isn't. What exactly do you mean when you say that
temp2 points to the number '32'?

Details of syntax are very important. "32", '32', and 32 are three
*very* different things; "32" is a string literal, '32' is a character
constant (whose meaning is implementation-defined; you almost
certainly don't want that), and 32 is an integer constant, of type
int.

temp2 cannot point to the *number* 32. (Well, there are ways that it
can, but I don't think that's what you're trying to do.) If you
wanted to point to a number, you'd need something of type int*, not
char*. (A char can hold a small number, but again, I don't think
that's what you're trying to do.)

As you can see, your description leaves open so many possiblities that
it's very hard to tell what you're really asking.

I'm going to *guess* that you mean that temp2 points to the string
"32", as in:

const char *temp2 = "32";

If that's correct, then temp2 points to a string; "32" is not a
number.
int samples;
//null

What do you mean by "null" here? All we can tell is that samples is a
variable of type int, and you haven't initialized it.
I need a statement that does this:

If (temp1 is equal to the word 'samples')
set samples to the value of temp2 (32).

Remember the distinction between single quotes and double quotes. I
think you mean "samples", not 'samples'.

What is temp1? You've declared temp and temp2; I see nothing called
temp1. I'm going to guess that you meant to say temp rather than
temp1, but the more you make us guess, the harder it is to help you.
So far I got (with your help)

if (strcmp(temp, "samples") == 0) //if temp == samples
{
//makes samples = temp2

}

And now we're back to temp again.
but im unsure of how to get the middle statement. Thanks again guys I
hope this is clearer. I reduced my code down to what i thought was
minimal but looking back it wasn't enough code to work off of
initially. Thanks!

Ok, if all my guess so far have been correct, then samples is a
variable of type int, and temp2 (which is of type char*) points to the
string "32". Given that, you want to assign the int value 32 to
samples. Is that correct?

If so, an assignment or a cast won't do the job. What you have is a
string, a sequence consisting of the characters '3', '2', '\0' (the
'\0' is a null character, the implicit string terminator). What you
want is the integer value 32. There's no operator built into the
language that will perform this translation for you -- but there are
routines in the standard library that will do the job. The simplest
one is atoi(), but I wouldn't advise using it except in a
quick-and-dirty program to be thrown away after you're done with it;
it does no real error checking.

The strtol function is the "right" way to do this. Using it is
moderately complex, but it does all the right error checking (for
example, if the input string doesn't look like a number, or if it does
but the value is too big, then strtol can tell you). The atoi
function is defined to work just like strtol except that any error
information is thrown away.

You might consider using atoi for a first draft of your program, just
to make sure you've got the logic right, then modifying it to use
strtol. Your system should include documentation for both functions.
Read it.

I realize it may seem that I'm being incredibly picky, but your
compiler is going to be just as picky, without being willing to guess
what you really meant.
 
K

Keith Thompson

I have:

char *temp;
//Lets say that this is pointing to the word "Samples"

char *temp2
//Lets say that this is pointing to the number '32'

int samples;
//null

I need a statement that does this:

If (temp1 is equal to the word 'samples')
set samples to the value of temp2 (32).


So far I got (with your help)


if (strcmp(temp, "samples") == 0) //if temp == samples
{
//makes samples = temp2

}

but im unsure of how to get the middle statement. Thanks again guys I
hope this is clearer. I reduced my code down to what i thought was
minimal but looking back it wasn't enough code to work off of
initially. Thanks!

One more thing I forgot to mention. I see that you have a variable
named samples, and a string with the value "samples". You should be
aware that there's no way to get from one to the other, unless you
explicitly code it yourself; variable names exist only in your C
source file, not in the program itself. I think you already know, but
I thought I should mention it.
 
T

tom lewton

Nothing, particularly, except that it is identical in meaning to
strcmp(temp, "samples") - am I to take it that you enjoy typing?

you seem to enjoy typing yourself, if the code you post here is anything
to go on: frequent verbosity along the lines of

if(p!=NULL) {
singlestatement();
}

instead of

if(p)
singlestatement();
 
R

Richard Heathfield

tom lewton said:
you seem to enjoy typing yourself, if the code you post here is
anything to go on: frequent verbosity along the lines of

if(p!=NULL) {
singlestatement();
}

instead of

if(p)
singlestatement();

Yes, it does seem like that, doesn't it? But in my defence I offer the
following:

1) I reserve my use of if(foo) to situations where foo is clearly
supposed to represent a Boolean concept - for example, I'll happily
write if(isprint((unsigned char)ch) and be glad of the saving. But
since pointers are not an on/off thing, I prefer not to pretend they
are, when using "if". I do this for readability reasons; I do find that
it makes the code quicker to read and understand.

2) It is not uncommon for people to maintain this:

if(condition)
singlestatement();

into this:

if(condition)
singlestatement();
otherstatement();

when what they intended was:

if(condition)
{
singlestatement();
otherstatement();
}

I have found it easier to put the braces in right at the start, as it
saves me debugging time later.

So whilst I agree that my style does cost extra typing time, I am
content that it saves me time overall.

In the OP's case, however, we had a construct that was in fact
equivalent to a strcmp, but this was not self-evident - it took longer
to write *and* longer to read, and I suspect it would take longer to
maintain, too.
 

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
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top