zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    鼠标效果
    全选与全不选
    正则表达式
    下拉菜单
    图片轮播
    弹出层
    间隔与延时
    JS基础
    引入
    样式表 文字
  • 原文地址:https://www.cnblogs.com/lexus/p/2578674.html
Copyright © 2011-2022 走看看