Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to understand this line of c ?

Reply
Thread Tools

How to understand this line of c ?

 
 
Red Cent
Guest
Posts: n/a
 
      01-04-2006
Keith Thompson writes:

>I knew that only because I happened to read your response immediately
>after Kenneth's article. Please read <http://cfaj.freeshell.org/google/>
>and follow its advice *before* you post here again; it's important.


Thanks for the pointer!

>If you're talking about something like interpreting the string pointed
>to by argv[i] as an integer, such as the string "123", that's not
>typecasting (or, more properly, casting).


I neglected to mention that I might cast one of the members of the
array.
If it's not casting, what is it? Converting?

--
Red Cent

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      01-04-2006
"Red Cent" <(E-Mail Removed)> writes:
> Keith Thompson writes:

[snip]
>>If you're talking about something like interpreting the string pointed
>>to by argv[i] as an integer, such as the string "123", that's not
>>typecasting (or, more properly, casting).

>
> I neglected to mention that I might cast one of the members of the
> array.
> If it's not casting, what is it? Converting?


Casting refers to an explicit cast operator, such as (type)expr.
Converting refers to a conversion from one type to another; this is
the operation performed by the cast operator.

If you're talking about converting one of the members of the argv
array (i.e., a pointer) to an integer type, that doesn't make much
sense. If you're talking about translating a string value to an
integer value, such as "123" --> 123, that's not what "conversion"
usually means.

Can you provide an example, in C code, of what you're talking about?

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
 
Chuck F.
Guest
Posts: n/a
 
      01-04-2006
(E-Mail Removed) wrote:
>
> Not so much treat argv as an int, but typecast it to another
> variable if needed (as a number)
>

I have no idea how this came up, due to the total lack of context
(see my sig below for proper reply technique using the broken
google usenet interface).

At any rate, why would you wish to cast it to anything else, when
it is known to be a pointer to a pointer to a char? You can't
change that fact.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
Reply With Quote
 
Red Cent
Guest
Posts: n/a
 
      01-04-2006
Keith Thompson writes:
>Can you provide an example, in C code, of what you're talking about?


(in reference to casting a string to an int)

Off the top of my head, I can't remember when I've used this. Vaguely
I remember having a program that needed to run X number of times. So I
passed an argument "123" which main's argv got.

Now in order to run it that many times, I needed to cast "123" to an
int 123, so I could use it in a counting loop.

If I get a chance, I'll look through some of my source code to see
where I've used it.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-05-2006
"Red Cent" <(E-Mail Removed)> writes:
> Keith Thompson writes:
>>Can you provide an example, in C code, of what you're talking about?

>
> (in reference to casting a string to an int)
>
> Off the top of my head, I can't remember when I've used this. Vaguely
> I remember having a program that needed to run X number of times. So I
> passed an argument "123" which main's argv got.
>
> Now in order to run it that many times, I needed to cast "123" to an
> int 123, so I could use it in a counting loop.


Ok, there's where the confusion is coming from.

A cast is a very specific thing; it's a C operator that specifies a
conversion from one type to another. It's possible to convert a
pointer to an integer, but it's seldom useful. For example, if your
program's first argument happens to be the string "123", casting
argv[1] to int: (int)argv[1], will give you an integer representation
of the address at which the string "123" is stored in memory.

What you're doing can be called a conversion, but it's not really a
conversion in the sense usually used in C. You probably do something
like passing the value of argv[1] (or argv[i]) to the atoi() or
strtol() function.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Daniel Rudy
Guest
Posts: n/a
 
      01-05-2006
At about the time of 1/3/2006 3:48 AM, pemo stated the following:

> "Daniel Rudy" <(E-Mail Removed)> wrote in message
> news:aHiuf.5705$(E-Mail Removed) ...
>
>>At about the time of 12/31/2005 11:27 AM, pemo stated the following:
>>
>>>"lnzju" <(E-Mail Removed)> wrote in message
>>>news:(E-Mail Removed) egroups.com...
>>>
>>>
>>>>main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1);}
>>>
>>>
>>>Here it is a little clearer [hopefully?]
>>>
>>>#include <stdio.h>
>>>
>>>// x will be 1 (the name of this app would be in argv[0]) if we
>>>// don't invoke the app with some args.
>>>//
>>>main(x)
>>>{
>>> char c;
>>>
>>> // zero then!
>>> //
>>> --x;
>>>
>>> while(c = "J!Mpwf!Zpv\1"[x])
>>> {
>>> // No need for the -1 if we add 1 to the literal's characters...
>>> //
>>> // "I Love You\1"
>>> //
>>> c = c - 1;
>>>
>>> x++;
>>>
>>> putchar(c);
>>> }
>>>}
>>>
>>>

>>
>>You know, the code that the OP provided is the reason why I hate
>>wannabe's trying to be hack programmers to impress the rest of us.
>>Anyone who codes like that needs to be shot, not once, not twice, but
>>thrice. Perferably in the groin.

>
>
> wannabes - who, me, them, you?
>
>


Who ever wrote that code. Granted, I'm fairly new to C myself, so I
could be considered a wannabe...But you will not catch me coding like that.


--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
 
Reply With Quote
 
pemo
Guest
Posts: n/a
 
      01-05-2006

"Daniel Rudy" <(E-Mail Removed)> wrote in message
news:Nm5vf.44114$(E-Mail Removed). com...
> At about the time of 1/3/2006 3:48 AM, pemo stated the following:
>
>> "Daniel Rudy" <(E-Mail Removed)> wrote in message
>> news:aHiuf.5705$(E-Mail Removed) ...
>>
>>>At about the time of 12/31/2005 11:27 AM, pemo stated the following:
>>>
>>>>"lnzju" <(E-Mail Removed)> wrote in message
>>>>news:(E-Mail Removed) legroups.com...
>>>>
>>>>
>>>>>main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1);}
>>>>
>>>>
>>>>Here it is a little clearer [hopefully?]
>>>>
>>>>#include <stdio.h>
>>>>
>>>>// x will be 1 (the name of this app would be in argv[0]) if we
>>>>// don't invoke the app with some args.
>>>>//
>>>>main(x)
>>>>{
>>>> char c;
>>>>
>>>> // zero then!
>>>> //
>>>> --x;
>>>>
>>>> while(c = "J!Mpwf!Zpv\1"[x])
>>>> {
>>>> // No need for the -1 if we add 1 to the literal's characters...
>>>> //
>>>> // "I Love You\1"
>>>> //
>>>> c = c - 1;
>>>>
>>>> x++;
>>>>
>>>> putchar(c);
>>>> }
>>>>}
>>>>
>>>>
>>>
>>>You know, the code that the OP provided is the reason why I hate
>>>wannabe's trying to be hack programmers to impress the rest of us.
>>>Anyone who codes like that needs to be shot, not once, not twice, but
>>>thrice. Perferably in the groin.

>>
>>
>> wannabes - who, me, them, you?
>>
>>

>
> Who ever wrote that code. Granted, I'm fairly new to C myself, so I
> could be considered a wannabe...But you will not catch me coding like
> that.


I don't think it was meant to be taken seriously, i.e., it's a bit of
obfuscated fun, and possibly started off life somewhere under
http://www.ioccc.org/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Read a file line by line and write each line to a file based on the5th byte scad C++ 23 05-17-2009 06:11 PM
How to read a text file line by line and remove some line kaushikshome C++ 4 09-10-2006 10:12 PM
Read all of this to understand how it works. then check around on otherRead all of this to understand how it works. then check around on other thelisa martin Computer Support 2 08-18-2005 06:40 AM
Read a file line by line with a maximum number of characters per line Hugo Java 10 10-18-2004 11:42 AM



Advertisments