Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5944835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:35:46+00:00 2026-05-22T16:35:46+00:00

I’ve forgotten how count a double dimension C array because I don’t understand why

  • 0

I’ve forgotten how count a double dimension C array because I don’t understand why this code return me a count of 12 instead of 6.

// My tab
static NSString *kStringTag[][2] = {
    {@"string1", @"1"},
    {@"string2", @"1"},
    {@"string3", @"0"},
    {@"string4", @"0"},
    {@"string5", @"1"},
    {@"string6", @"1"},
    {nil, nil}
};

// My C func
unsigned int tablen(void **tab)
{
    unsigned int i = 0;

    while (tab[i] != nil)
        i++;

    return i;
}

 - (void)viewDidLoad
{
    NSLog(@"%d", tablen((void **)kStringTab));
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:35:46+00:00Added an answer on May 22, 2026 at 4:35 pm

    Your code is not C.

    If it were C, tab would be an array of array of pointers to NSStrings (whatever that is).

    In C an array of arrays of pointers to NSStrings is not necessarily compatible with a pointer to pointer to void … so remove the casts and get the types correct.

    In C, this works …

    #include <stdio.h>
    
    static char *kStringTab[][2] = {
        {"string1", "1"},
        {"string2", "1"},
        {"string3", "0"},
        {"string4", "0"},
        {"string5", "1"},
        {"string6", "1"},
        {NULL, NULL},
    };
    
    unsigned int tablen(char *tab[][2]) {
      unsigned int i = 0;
      while (tab[i][0] != NULL) i++;
      return i;
    }
    
    int main(void) {
      printf("%d\n", tablen(kStringTab));
      return 0;
    }
    

    Suggestion: increase the warning level of your compiler and mind the warnings.


    Edit: new generic version

    #include <math.h>
    #include <stdio.h>
    
    static double anothertest[][3] = {
        {42, 54, -122},
        {33, -0.001, 0.001},
        {6, 0, 7},             /* 0 in middle: stop condition in nullp2 :) */
        {2, 2, 2},
    };
    
    static char *kStringTab[][2] = {
        {"string1", "1"},
        {"string2", "1"},
        {"string3", "0"},
        {"string4", "0"},
        {"string5", "1"},
        {"string6", "1"},
        {NULL, NULL},
    };
    
    int nullp2(const void *elem) {
      const double *tmp = elem;
      return (fabs(tmp[1]) < 0.000000001);
    }
    
    int nullp(const void *elem) {
      char (*const *tmp)[2] = elem; /* tmp is a pointer to each element of kStringTab */
      return ((*tmp)[0] == NULL);
    }
    
    unsigned int tablen(void *x, size_t size,
                        int (*check)(const void *)) {
      char *y = x;
      unsigned int i = 0;
    
      while (!check(y)) {
    
        i++;
        y += size;
      }
      return i;
    }
    
    int main(void) {
      printf("tablen returns %d\n",
            tablen(kStringTab, sizeof *kStringTab, nullp));
      printf("tablen returns %d\n",
            tablen(anothertest, sizeof *anothertest, nullp2));
      return 0;
    }
    

    You can see it running at ideone.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.