Thread: Help using a 2D array of strings as a function argument

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    10

    Help using a 2D array of strings as a function argument

    I've got a program that reads a bunch of tweets from a .txt file into a 2D char array. Each tweet is a new row in the array, while the characters in each string make up the columns (or at least I think that's how it works). I'm trying to write a function that will find the index of the array that contains the tweet with the greatest amount of characters (or longest string). This isn't a full representation of the program, but here is what I've got so far:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <limits.h>
    #include <ctype.h>
    
    #define MAX_TWEET_SIZE 200
    #define MAX_TWEETS 1000
    
    int max_index(array[][MAX_TWEET_SIZE], int tweets);
    
    int main(int argc, char* argv[]){
        char tweets[MAX_TWEETS][MAX_TWEETS_SIZE];
        int num_tweets = 0;
    
        int i = 0;
        while(fgets(tweets[i], MAX_TWEET_SIZE, tweetFile) != NULL){
            i++;
            num_tweets++;
        }
    
        printf("The longest tweet is #%d.\n", max_index(tweets, num_tweets));
    
        (end of main function to this point)
    }
    
    
    int max_index(char array[][MAX_TWEET_SIZE], int tweets){
        int i, index;
        for(i = 1; i <= tweets; i++){
            if(strlen(array + i) > strlen(array + i - 1)){
            index = i;
            }
        }
        
        return index;
    }
    I'm still fairly new to programming and dealing with pointers is pretty confusing for me at this point. I understand that my usage of 'strlen()' is probably not correct as that is only error message I'm getting when I compile. I've been looking around to try and understand strlen() a little better but I can't find any examples that fit my problem here, and I have just hit a wall. I'm not looking to be handed an answer, but if someone has some advice to help me understand how to get this program working it'd be greatly appreciated!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What exactly does your error message tell you? Please post the complete error message, all of them, exactly as they appear in your development environment. These messages have important information to aid in finding and fixing the problems and you really need to learn your compiler's language to be able to understand the messages.


    Jim

  3. #3
    Registered User
    Join Date
    Sep 2016
    Posts
    10
    Ok here's the error message:
    C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/string.h:54:18: note: expected 'const char *' but argument is of type 'char (*)[200]'
    size_t __cdecl strlen(const char *_Str);
    ^
    tweetFunctions.c:36:35: warning: passing argument 1 of 'strlen' from incompatible pointer type
    if(strlen(array + i) > strlen(array + i - 1)){


    So I tried type-casting 'array' with '(const char *)array' inside strlen() to give it what it expects . It compiles but won't run properly.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    So I tried type-casting 'array' with '(const char *)array' inside strlen() to give it what it expects . It compiles but won't run properly.
    Type casting just to shut up the compiler is usually not the correct way to proceed. Type casting should be the last tool in your tool box.

    The solution here is to actually pass the address of the C-string, not the address of the array of C-string.

    Code:
        //for(i = 1; i <= tweets; i++) // Warning arrays in C start at zero not 1 and end at size - 1 not size, probable out of bounds array access.
        index = 0;
        for(i = 1; i < tweets; ++i)
       {
            //if(strlen(array + i) > strlen(array + i - 1)) // Use array notation!
            if(strlen(array[i]) > strlen(array[index])
           {
               index = i;
    Jim

  5. #5
    Registered User
    Join Date
    Sep 2016
    Posts
    10
    Ok great. Glad to know I wasn't quite as far off as I thought I was. I thought I had to use pointer notation in the strlen() function. But I guess 'array[i]' is a pointer anyway... right? Thanks so much for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array pointer as function argument?
    By mtn_student in forum C Programming
    Replies: 1
    Last Post: 09-27-2011, 03:25 PM
  2. multidimensional array as a function argument
    By Luciferek in forum C++ Programming
    Replies: 7
    Last Post: 09-30-2008, 11:16 AM
  3. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  4. function passing argument..array ?
    By jochen in forum C Programming
    Replies: 2
    Last Post: 09-30-2007, 11:53 AM
  5. How to pass an array of strings as a function argument?
    By Nazgulled in forum C Programming
    Replies: 7
    Last Post: 04-02-2007, 10:38 AM

Tags for this Thread