public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Newbie Hello
@ 2004-08-15  2:08 Larry Brown
  2004-08-15  9:30 ` Ishwar Rattan
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Larry Brown @ 2004-08-15  2:08 UTC (permalink / raw)
  To: gcc-help

I hope I'm in the right place... I am a fairly accomplished php
developer and have written quite a bit of code with it.  However, all of
my coding experience has been with scripting languages and I have never
had to deal with memory allocation and rarely ever dealt with pointers
or casting etc.

For instance...

with scripting I can simply access arguments by referencing the string
at argv[1] or ARGV[1] etc.  It looks like I should be able to do this
with c but I have to reference *argv[x] and *argv[x] only holds the
first character.  The following is a snippet...

int main(int argc, *argv[])
{
	int secondVar=*argv[2];
}

if the second argument is say ... 10, I only get the 1.  There is some
logic that I must follow that I can't see.  I've tried looking at
*argv[2][0] to see if it was one and *argv[2][1] was zero but is
aparently not the case.

I have looked at several howto/instruction documents and none seem to
yeild much.  

TIA

Again, I hope I'm in the right place...

Larry

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Newbie Hello
  2004-08-15  2:08 Newbie Hello Larry Brown
@ 2004-08-15  9:30 ` Ishwar Rattan
  2004-08-15 16:57 ` Jeffrey Holle
  2004-08-16 11:26 ` Arno Wilhelm
  2 siblings, 0 replies; 5+ messages in thread
From: Ishwar Rattan @ 2004-08-15  9:30 UTC (permalink / raw)
  To: Larry Brown; +Cc: gcc-help

Locate a basic C programming txt and read it carefully
or stick with scripting..

-ishwar


On Sat, 14 Aug 2004, Larry Brown wrote:

> I hope I'm in the right place... I am a fairly accomplished php
> developer and have written quite a bit of code with it.  However, all of
> my coding experience has been with scripting languages and I have never
> had to deal with memory allocation and rarely ever dealt with pointers
> or casting etc.
>
> For instance...
>
> with scripting I can simply access arguments by referencing the string
> at argv[1] or ARGV[1] etc.  It looks like I should be able to do this
> with c but I have to reference *argv[x] and *argv[x] only holds the
> first character.  The following is a snippet...
>
> int main(int argc, *argv[])
> {
> 	int secondVar=*argv[2];
> }
>
> if the second argument is say ... 10, I only get the 1.  There is some
> logic that I must follow that I can't see.  I've tried looking at
> *argv[2][0] to see if it was one and *argv[2][1] was zero but is
> aparently not the case.
>
> I have looked at several howto/instruction documents and none seem to
> yeild much.
>
> TIA
>
> Again, I hope I'm in the right place...
>
> Larry
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Newbie Hello
  2004-08-15  2:08 Newbie Hello Larry Brown
  2004-08-15  9:30 ` Ishwar Rattan
@ 2004-08-15 16:57 ` Jeffrey Holle
  2004-08-16 11:26 ` Arno Wilhelm
  2 siblings, 0 replies; 5+ messages in thread
From: Jeffrey Holle @ 2004-08-15 16:57 UTC (permalink / raw)
  To: gcc-help

The thing that you need to know about the difference between a scripting 
language and C/C++ is that the later is strongly typed.

This means that you basically need to know what these types are and what 
can and can't be auto converted.

In your case, you are treating a *char as an int.

If you are using C++, the most eligant way to do this is via 
boost::lexical_cast.  In C, there are more primative facilities.  Look 
for atoi documentation.

Larry Brown wrote:
> I hope I'm in the right place... I am a fairly accomplished php
> developer and have written quite a bit of code with it.  However, all of
> my coding experience has been with scripting languages and I have never
> had to deal with memory allocation and rarely ever dealt with pointers
> or casting etc.
> 
> For instance...
> 
> with scripting I can simply access arguments by referencing the string
> at argv[1] or ARGV[1] etc.  It looks like I should be able to do this
> with c but I have to reference *argv[x] and *argv[x] only holds the
> first character.  The following is a snippet...
> 
> int main(int argc, *argv[])
> {
> 	int secondVar=*argv[2];
> }
> 
> if the second argument is say ... 10, I only get the 1.  There is some
> logic that I must follow that I can't see.  I've tried looking at
> *argv[2][0] to see if it was one and *argv[2][1] was zero but is
> aparently not the case.
> 
> I have looked at several howto/instruction documents and none seem to
> yeild much.  
> 
> TIA
> 
> Again, I hope I'm in the right place...
> 
> Larry
> 
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Newbie Hello
  2004-08-15  2:08 Newbie Hello Larry Brown
  2004-08-15  9:30 ` Ishwar Rattan
  2004-08-15 16:57 ` Jeffrey Holle
@ 2004-08-16 11:26 ` Arno Wilhelm
  2 siblings, 0 replies; 5+ messages in thread
From: Arno Wilhelm @ 2004-08-16 11:26 UTC (permalink / raw)
  To: Larry Brown; +Cc: gcc-help

On Sun, 2004-08-15 at 00:30, Larry Brown wrote:
> I hope I'm in the right place... I am a fairly accomplished php
> developer and have written quite a bit of code with it.  However, all of
> my coding experience has been with scripting languages and I have never
> had to deal with memory allocation and rarely ever dealt with pointers
> or casting etc.
> 
> For instance...
> 
> with scripting I can simply access arguments by referencing the string
> at argv[1] or ARGV[1] etc.  It looks like I should be able to do this
> with c but I have to reference *argv[x] and *argv[x] only holds the
> first character.  The following is a snippet...
> 
> int main(int argc, *argv[])
> {
> 	int secondVar=*argv[2];
> }
> 
> if the second argument is say ... 10, I only get the 1.  There is some
> logic that I must follow that I can't see.  I've tried looking at
> *argv[2][0] to see if it was one and *argv[2][1] was zero but is
> aparently not the case.
> 
> I have looked at several howto/instruction documents and none seem to
> yeild much.  
> 
> TIA
> 
> Again, I hope I'm in the right place...
> 
> Larry


Try this:

main.c:
-------

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3
      4 int main( int argc, char * argv[] )
      5 {
      6
      7 char * stringVar = argv[1];
      8 printf( "stringVar = %s\n", stringVar );
      9 int numberVar = atoi( stringVar );
     10 printf( "numberVar = %d\n", numberVar );
     11
     12
     13 return 0;
     14 }


> gcc -g -Wall -o args main.c
> ./args 10
stringVar = 10
numberVar = 10
>


-- 
Arno Wilhelm <arno.wilhelm@profile.co.at>
proFILE Computersysteme GmbH

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: Newbie Hello
@ 2004-08-16 17:14 lrtaylor
  0 siblings, 0 replies; 5+ messages in thread
From: lrtaylor @ 2004-08-16 17:14 UTC (permalink / raw)
  To: larry.brown, gcc-help

Larry,

Ishwar, while rude(!), has a good point that you should get a good
beginners book on C or C++ programming.  For C, I might recommend "The C
Programming Language" by Kernighan and Ritchie.  While not a beginners
book, it's a good, concise reference to the language suitable for
experienced developers.  The reason for this recommendation is that C
and C++ handle things like strings much differently than scripting
languages do, and you need to understand how this works in order to be
able to effectively use them.

In this case, what's really happening is that your command line
arguments are being passed into your main function in an array of
strings (C-style strings - that is, character arrays), where each array
element of argv is a pointer that points to the first character of that
null-terminated string.  So, argv[0] points to the first character of
the first argument.  Adding the dereference operator dereferences that
pointer and returns that first character.  To use the whole string, you
would pass the pointer to that string to functions that can manipulate
strings.  For example, if you are expecting a number, you can use the
atoi function (ASCII to integer) like this:

int num = atoi(argv[1]);

Doing something like *argv[1] will return the first character of that
argument.  If the user entered '10', then it would return the ASCII
character for '1', not the value 1.

Hope this helps a bit.

Thanks,
Lyle


-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
Behalf Of Larry Brown
Sent: Saturday, August 14, 2004 4:30 PM
To: gcc-help@gcc.gnu.org
Subject: Newbie Hello

I hope I'm in the right place... I am a fairly accomplished php
developer and have written quite a bit of code with it.  However, all of
my coding experience has been with scripting languages and I have never
had to deal with memory allocation and rarely ever dealt with pointers
or casting etc.

For instance...

with scripting I can simply access arguments by referencing the string
at argv[1] or ARGV[1] etc.  It looks like I should be able to do this
with c but I have to reference *argv[x] and *argv[x] only holds the
first character.  The following is a snippet...

int main(int argc, *argv[])
{
	int secondVar=*argv[2];
}

if the second argument is say ... 10, I only get the 1.  There is some
logic that I must follow that I can't see.  I've tried looking at
*argv[2][0] to see if it was one and *argv[2][1] was zero but is
aparently not the case.

I have looked at several howto/instruction documents and none seem to
yeild much.  

TIA

Again, I hope I'm in the right place...

Larry

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2004-08-16 16:44 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-08-15  2:08 Newbie Hello Larry Brown
2004-08-15  9:30 ` Ishwar Rattan
2004-08-15 16:57 ` Jeffrey Holle
2004-08-16 11:26 ` Arno Wilhelm
2004-08-16 17:14 lrtaylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).