How to determine the end of a string array

The way to end the array charbefore it becomes full is to put it '\0'at the end, for example,

single_str[5] ='\0';

Then how to finish the 2D array charthis way?

+4
source share
4 answers

In practice, you should avoid thinking about 2D arrays in C. Stricto sensu, C does not know about two-dimensional arrays, only about arrays of arrays (fixed length, all of the same size) or about arrays of pointers (or an array of aggregates or scalars).

. C . ( , .. const char*), NULL

const char*const arrstr[] = {
   "Hello",
   "Nice",
   "World",
   NULL
};

sizeof(const char*) 8, sizeof(arrstr) 32; sizeof("Nice") 5.

for (const char*const* p = arrstr; *p; p++) printf("%s\n", *p);

C99 , ,

struct my_string_array_st {
   unsigned len;
   char* arr[]; // array of len pointers */
};

, ,

struct my_string_array_st* make_string_array(unsigned ln) {
  struct my_string_array_st*p
     = malloc(sizeof(struct my_string_array_st) + len*sizeof(char*));
  if (!p) { perror("malloc"); exit(EXIT_FAILURE); };
  p->len = ln;
  for (unsigned ix=0; ix<ln; ix+) p->arr[ix] = NULL;
  return p; }

( ), strdup, . ( )

void 
set_string_array(struct my_string_array_st*p, unsigned ix, const char*str) {
  if (!p || ix>=p->len) return;
  free(p->arr[ix]);
  char* s = NULL;
  if (str) {
     s = strdup(str);
     if (!s) { perror("strdup"); exit(EXIT_FAILURE); };
  };
  p->arr[ix] = s;
}

(, (3) NULL, ) , .

void destroy_string_array(struct my_string_array_st*p) {
  if (!p) return;
  unsigned l = p->len;
  for (unsigned ix=0; ix<l; ix++) free(p->arr[ix]);
  free (p);
}

, :

const char* nth_string_array(struct my_string_array_st*p, unsigned ix)
{
  if (!p || ix>=p->len) return NULL;
  return p->arr[ix];
}
+4

"":

#include <stdio.h>

int main(void)
{
    char arr[][3] = {"ab", "cd", ""};
    char (*p)[3] = arr;

    while (**p) { /* while not an empty string */
        printf("%s\n", *p);
        p++;
    } 
    return 0;
}

arr , , , ETX ( ):

#include <stdio.h>

int main(void)
{
    char arr[][3] = {"ab", "", "\x03"};
    char (*p)[3] = arr;

    while (**p != '\x03') {
        printf("%s\n", *p);
        p++;
    } 
    return 0;
}

NULL:

#include <stdio.h>

int main(void)
{
    char *arr[] = {"ab", "cd", NULL}; /* Read only */
    char **p = arr;

    while (*p) { /* while not NULL */
        printf("%s\n", *p);
        p++;
    } 
    return 0;
}
+2

.

C-, , , play-load .

NULL .

:

#define END_OF_ARRAY "__EOA__" 

:

#define END_OF_ARRAY "" 

, C- "" !

char array [][8] = {
  "1st",
  "2nd",
  END_OF_ARRAY
}

, ​​:

ssize_t get_array_size(const char (*parray)[][8])
{
  ssize_t s = -1;

  if (NULL == parray)
  {
    errno = EINVAL;
  }
  else
  {
    s = 0;

    while (strcmp((*parray)[s], END_OF_ARRAY))
    {
      ++s;
    }
  }

  return s;
}

:

#define END_OF_ARRAY NULL

#define END_OF_ARRAY "__EOA__" 

, NULL.

char * array[] = {
  "1st",
  "2nd",
  END_OF_ARRAY 
}

, ​​:

ssize_t get_array_size(const char *** parray)
{
  ssize_t s = -1;

  if (NULL == parray || NULL == *parray)
  {
    errno = EINVAL;
  }
  else
  {
    s = 0;

    while ((*parray)[s] != END_OF_ARRAY)
    {
      ++s;
    }
  }

  return s;
}

:

int main(void)
{
  ssize_t result = get_array_size(&array);
  if (-1 == result)
  {
    perror("get_array_size() failed");
  }
  else
  {
    size_t size = result;
    printf("number of elements: %zu\n", size)
  }

  return 0;
}

:

#define END_OF_ARRAY NULL

#define END_OF_ARRAY "__EOA__" 

, NULL.

char * array[] = {
  "1st",
  "2nd",
  END_OF_ARRAY 
}

, ​​:

ssize_t get_array_size(const char ** parray)
{
  ssize_t s = -1;

  if (NULL == parray)
  {
    errno = EINVAL;
  }
  else
  {
    s = 0;

    while (parray[s] != END_OF_ARRAY)
    {
      ++s;
    }
  }

  return s;
}

, , :

int main(void)
{
  ssize_t result = get_array_size(array);
  if (-1 == result)
  {
    perror("get_array_size() failed");
  }
  else
  {
    size_t size = result;
    printf("number of elements: %zu\n", size)
  }

  return 0;
}
+2

.

  char ar[3][20];
  ar[2][0]='\0';

. , 1th line 10th, .

  ar[1][10]='\0';

, , .

+1

All Articles