Thread: Passing Array of Strings to a function ??

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    As Salem pointed out, 2D char arrays on the stack, are perfectly modifiable.

    Of course there are differences passing an array with a different number of dimensions, to a function.

  2. #17
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I'm sorry Adak. I have added some egg to my face to compensate.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #18
    Registered User
    Join Date
    Dec 2008
    Posts
    12
    Ok thanks for your help everyone I'll try to implement what you've all said but I'll need to understand it first. I probably should have mentioned that I'm a begginer to C and I'm used to programming in Java where arrays are more easily manipulated. Even in java I would be considered a novice. The 2nd version of my program works but I can't understand why it returns -1 when it goes to the last else can anyone explain. I realise this may be trivial but I'm learning a lot from just trying differnt things.

    Oh and my indentation wasn't great but I don't think I deserve a spanking.. i'll try harded next time I promise....

    Edit: Ok just removed implicit main, inserted the 'const char *note' and the warnings are gone. I understand that using main() is the same as int main(void) but that the later is safer. Thanks. As for the scanf thing i'm looking into fgets. Thanks Again
    Last edited by Taper; 12-07-2008 at 01:10 PM.

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    @MK27 - no problem. C is a complex language and nobody learning it, is going to be right all the time - certainly not me.

    @Taper, C is a concise language. We don't use i = i + 1, we use ++i or i++. We get a lot of code, onto a single page, using good formatting and style.

    I hate seeing code that has friggin' 15 letter-width tab indentation. The program frequently will "break the forum tables", making it almost unreadable.

    Code:
    for(i = 0; i < MaxArraySize - 1; i++)
    
    
    
    
                   {
    
    
    
    
                                   for(j = i + 1; j < MaxArraySize; j++)
    
    
    
    
                                                  {
    
    
    
    
                                                                  if(strcmp(struct.name[i], struct.name[j]) > 0) 
    
    
    
    
                                                                                 {        
                                                                                                  //etc.
    
    
    
    for(i = 0; i < MaxArraySize - 1; i++)
    {  
        for(j = i + 1; j < MaxArraySize; j++)
       {
           if(strcmp(struct.name[i], struct.name[j]) > 0) 
          {        
             //etc.
          }
       }
    }

    The first example (all part of a selection sort btw), is very likely to "break" the tables, and
    has no more info than the smaller more C like, example below it.

    In one screenful, with code like the second example, you can see a LOT of what's going on.

    You can't do that with the first example, and I urge you to leave it behind ASAP. Nothing says "look at me riding my big wheel!" more, than code that is like that first example.

    Teachers won't put up with it, and neither will anyone in the programming community or industry.
    Last edited by Adak; 12-07-2008 at 01:24 PM.

  5. #20
    Registered User
    Join Date
    Dec 2008
    Posts
    12
    Point taken mate. I will indent correctly before posting from now on.

    Oh anyone any idea why, when the function calls itself again it doesn't return what the user entered?
    Last edited by Taper; 12-07-2008 at 03:06 PM.

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Taper View Post
    ...I understand that using main() is the same as int main(void) but that the later is safer...
    That's not quite true.

    Take this example:
    Code:
    void foo();
    void foo(void);
    These are prototypes, but they are different. The first take an unlimited amount of arguments, the second takes none.

    Let's take a look at another:
    Code:
    void foo () { }
    void foo(void) { }
    In this case, they are equal. The "void" part only matters in the prototype.
    And since you only define or implement main, it doesn't matter whether you put void into the parameter list or not.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function passing argument..array ?
    By jochen in forum C Programming
    Replies: 2
    Last Post: 09-30-2007, 11:53 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 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. passing array of structures to function
    By bvnorth in forum C Programming
    Replies: 3
    Last Post: 08-22-2003, 07:15 AM