Passing string length to another file

This is a discussion on Passing string length to another file within the C Programming forums, part of the General Programming Boards category; Suppose I have: Code: len = strlen(command) and in another file, I have a function Code: int (char array[]) { ...

  1. #1
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463

    Passing string length to another file

    Suppose I have:

    Code:
    len = strlen(command)
    and in another file, I have a function

    Code:
     int (char array[]) {
    How do I pass that strlen, without adding extra parameters to the function? I first did:

    Code:
    int (char array[len]) {
    But that gave me undeclared variable error or something,

    I was thinking of passing the strlen into a struct in the other file, but I'm not entirely sure on how to do that.
    =========================================
    Everytime you segfault, you murder some part of the world

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Why not add an extra argument? That's the best and easiest way to do it.
    Otherwise you can create a struct and pass as argument.
    Avoid global variables.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Thanks, but I don't know how to pass it into a struct from a different file.

    Also, I just integrated both the files into one, less hassle
    =========================================
    Everytime you segfault, you murder some part of the world

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    You might do something like this:
    Code:
    A.h:
    
    struct mystruct
    {
        /* ... */
    };
    
    A.c:
    #include "A.h"
    void myfunc(struct mystruct* p)
    {
        /* ... */
    }
    
    B.c:
    #include "A.h"
    int main(void)
    {
        /* ... */
        struct mystruct mys;
        myfunc(&mys);
        /* ... */
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    You can either pass the length, or you can simply call strlen inside the function.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Thanks for the advice, will try it =)
    =========================================
    Everytime you segfault, you murder some part of the world

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 07:26 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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