From Perl to C

This is a discussion on From Perl to C within the C Programming forums, part of the General Programming Boards category; Hi everyone, I'm a Perl programmer trying to get into C, and was curious if there were better ways to ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    5

    From Perl to C

    Hi everyone, I'm a Perl programmer trying to get into C, and was curious if there were better ways to do the following few operations :

    First off, removing a newline. With the string
    $first in perl, i would simply type in chomp($first);

    From what I understand, in C, I would have to type something in like:
    first[strlen(first ) -1] = '\0';

    Seems to be kind of a pain in the butt - Is there a better way to do this, or am I going to end up just having to write a function to do this since I'd be calling it fairly often?

    Also, what do you do when you're dealing with a number being entered from some source where the variable could be either an int OR a float? Take the input in as a char and figure it out later, or? The whole int/float thing is really mind boggling, coming from perl

    Also, is there a regular expressions library by any chance? What's the standard practice for pattern matching in C?

    Thanks!

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    > From what I understand, in C, I would have to type something
    >in like: first[strlen(first ) -1] = '\0';
    This is how you have to do it. What you could do is create a function like this:
    Code:
    void RemoveNewLine(char* Line){
        Line[strlen(Line)-1]  = '\0';
    }
    
    //You then can call the function like this
    RemoveNewLine(String);
    You could also create a pre-processor macro and then call the macro.

    If you have some input as a string, you can turn it into a int using atoi and into a float with atof. What I would do is store it as a float or double and then using printf("%g", Variable). This way, if it is an int it will print the int part. If it is a float it will print it as far as there are significant digits.

    For example:
    Code:
    char String[128] = 10.64
    float Number = atof(String);
    //Do some manipulation with Number
    printf("%g\n", Number);
    This (without any other manipulation) would output 10.64.

    If you wanted to figure out if it was an int or float I would just compare atof and atoi and see if they are the same. If they are then its an int or else its a float.

    Hope that helps.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    5
    This is what I'm having trouble understanding... In this code:

    void RemoveNewLine(char* Line){
    Line[strlen(Line)-1] = '\0';
    }

    //You then can call the function like this
    RemoveNewLine(String);

    You call the function and pass the string to it. However, the function doesn't return anything. When passing strings into a function in C, is the variable now global? How does the value get returned? In perl, you'd have to return the variable back, otherwise it wouldn't be touched. Do you only need to return values when you're dealing with numbers?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >In perl, you'd have to return the variable back, otherwise it
    >wouldn't be touched. Do you only need to return values when
    >you're dealing with numbers?
    In C it's possible to simulate passing by reference by passing the address of the item being passed to the function. Since the pointer that the function recieves holds the address of the original item, you can modify the original without having to pass it back:
    Code:
    /* Pass by value */
    int func ( int arg )
    {
      return arg + 1;
    }
    
    int main ( void )
    {
      int one = 1;
      one = func ( one );
      printf ( "%d\n", one );
      return 0;
    }
    
    /* Pass by "reference" */
    void func ( int *arg )
    {
      *arg += 1;
    }
    
    int main ( void )
    {
      int one = 1;
      func ( &one );
      printf ( "%d\n", one );
      return 0;
    }
    Either way the end result is 2. Now, arrays must be passed as a pointer because no matter what notation you use, the compiler breaks it down into a char * to the first element of the array. So by passing an array to a function, you have simulated passing by reference by default and in some cases there's no need to return anything.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Pointers can be a difficult concept to follow. Just don't try to define an array in the called function and than return it, unless it is dynamically allocated. In C you have to know where the memory is being allocated, either on the global free store, or else the function stack. Don't let this confuse you more because you didn't make this mistake, it's just something to think about at a later time, because you will run into it. Try reading chapters on arrays and pointers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C structure in perl typemap
    By rotis23 in forum Linux Programming
    Replies: 1
    Last Post: 07-16-2003, 11:13 AM
  2. de facto perl book
    By rotis23 in forum Linux Programming
    Replies: 1
    Last Post: 05-22-2003, 04:43 AM
  3. perl program question
    By newbie2c in forum Tech Board
    Replies: 2
    Last Post: 02-03-2003, 09:19 AM
  4. Urgent Needed - PERL
    By beely in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 07-02-2002, 08:09 PM
  5. perl need help pls.....
    By magnum38 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 12-12-2001, 09:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21