Passing a character array to a function

This is a discussion on Passing a character array to a function within the C Programming forums, part of the General Programming Boards category; Hi all, I'm new to the boards and new to C, working my way through K&R and I've become stuck ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    2

    Passing a character array to a function

    Hi all,

    I'm new to the boards and new to C, working my way through K&R and I've become stuck on exercise 1-18. I've used a function to place an input line into a character array named line. main then attempts to pass the array to a new function, which is currently supposed to simply print the string (it'll do more later) - yet the string doesn't print.

    I suspect that the array in the second function isn't pointing to the correct mem location but I can't see what I'm doing wrong. I'd really appreciate any tips!!

    Code:
    #include <stdio.h>
    #define MAXLINE 1000 /* max line length */
    
    int getline(char s[], int maxline);
    void remspac(char t[], int len);
    
    /* Remove trailing whitespace from lines & remove blank lines */
    main()
    {
      int len; /* line length */
      char line[MAXLINE];
    
      while ((len = getline(line, MAXLINE)) > 0)
        ;
      remspac(line, len);
    
      return 0;
    }
    
    /* getline: reads a line into s, and returns the length (i) */
    int getline(char s[], int lim)
    {
      int i, c;
      for (i = 0; i < lim - 1 && (c=getchar()) != EOF && c!='\n'; ++i)
        s[i] = c;
      if (c == '\n') {
        s[i] = c;
        ++i;
      }
      s[i] = '\0';
      if (s[i-2] == ' ')
        printf("Trailing space\n");
    
      return i;
    }
    
    void remspac(char t[], int len)
    {
        printf("%s", t);
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    You start at index 0 every time getline() is called and you set len to that. So len in main() will only contain the length of the last string getline() reads in and the line will only consist of those characters. So if you type in MAXLINE+5 characters, the line will only have the last 5 that you typed. That's probably not what you want.

    Also, you can only break out of the input cycle by sending EOF. If you just press ENTER without typing anything before it, getline() will return 1 and then getline() is called again by main(). Is that what you were aiming for? If so, don't even test for '\n' because it seems pretty meaningless in its current implementation.

    I'm not sure what the goal of exercise 1-18 is. If I knew that then I could probably help more.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    2
    You start at index 0 every time getline() is called and you set len to that. So len in main() will only contain the length of the last string getline() reads in and the line will only consist of those characters. So if you type in MAXLINE+5 characters, the line will only have the last 5 that you typed. That's probably not what you want.
    Ah, so I should be calling remspac within the while loop that that generates line - I knew that but just messed up, seems to be genertaing the right result now but I'll need to check further.

    Also, you can only break out of the input cycle by sending EOF. If you just press ENTER without typing anything before it, getline() will return 1 and then getline() is called again by main(). Is that what you were aiming for? If so, don't even test for '\n' because it seems pretty meaningless in its current implementation.
    The code currently doesn't do a lot as I couldn't pass the array around, I should have removed the check for \n. Shouldn't getline return 2 for a blank line anyway? I thought the two characters would be \n and \0

    [EDIT] - Oooooops, it will return 1 - there will be a \0 but it's not counted by len.[/EDIT]


    The purpose of the exercise is to remove trailing whitespace (blanks and tabs) from lines, and delete blank lines (sorry, I should probably have stated this!!!). I should be ok if I've got the array moving around correctly but I'll post back if I come across further problems.

    Thanks a lot for the help!!!!
    Last edited by Qchem; 03-07-2005 at 06:14 AM.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    Yeah, you've got the array moving around correctly. The problem is just with what getline() does and the while() loop in which you're calling it.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Passing a pointer to two-dimension array in a function
    By E_I_S in forum C++ Programming
    Replies: 11
    Last Post: 06-19-2008, 09:57 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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