• How to get the length of array of strings in C? Yahoo! Answers


    How to get the length of array of strings in C? - Yahoo! Answers

    Best Answer - Chosen by Voters

    cja gave you one solution. In case you have a local array defintion,

    you can determine the number of elements using the sizeof operator.

    But what to do, in case you pass an array as a parameter to another

    function without telling that function how many elements it had:

    int fct(char **array)

    {

    int howmany = -1;

    }

    The solution for this problem is to NULL terminate the array you are using:

    i.e.:

    char *ar[] = {

    "Hello World!",

    "Good Night World!",

    "Here I sleep!",

    0

    };



    now what you can do is this:

    int fct(char **array)

    {

    char *ptr = array[0];

    int iCount = 0;

    while(*ptr) {

    ++iCount; // increments element counter

    ++ptr; // points to the next element.

    }

    return iCount;

    }

    Please note: this does not only work for strings, but for all type of objects (typically you would use a different terminator for numb
  • 相关阅读:
    Python学习笔记(三)
    Python学习笔记(二)
    GDUFE ACM1159
    GDUEFE ACM1003 练手
    GDUFE ACM1033
    GDUFE ACM1128
    GDUFE ACM1002
    EDUFE ACM1050
    GDUFE ACM1007
    GDUFE ACM1003
  • 原文地址:https://www.cnblogs.com/lexus/p/2578674.html
Copyright © 2020-2023  润新知