简体   繁体   中英

Can't put a string in a 2d array

I'm trying to create a function that takes a string as parameter and returns a table that contains in each compartments a word of the given string.

Here's my code:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int     nb_words(char *str)
{
  int   i;
  int   nb;

  i = 0;
  nb = 1;
  while (str[i] != '\0')
    {
      if (str[i] == ' ')
        nb++;
      i++;
    }
  return (nb);
}

void    my_show_wordtab(char **tab)
{
   int  i;

   i = 0;
   while (tab[i] != NULL)
      {
        printf("%s\n", tab[i]);
        i++;
      }
}

char    **put_in_tab(char *str, char **tab)
{
  int   i;
  int   j;
  int   k;

  i = 0;
  j = 0;
  if ((tab = malloc(sizeof(char *) * (nb_words(str) + 1))) == NULL)
    return (NULL);
  while (str[i] != '\0')
    {
      if ((tab[j] = malloc(sizeof(char) * (strlen(str) + 1))) == NULL)
        return (NULL);
      k = 0;
      while (str[i] != ' ' && str[i] != '\0')
        {
          tab[j][k] = str[i];
          k++;
          i++;
        }
      tab[j][k] = '\0';
      j++;
      i++;
    }
  tab[j] = NULL;
  my_show_wordtab(tab);
  return (tab);
}

int     main(int ac, char **av)
{
  char  **tab;

  if (ac != 2)
    return (1);
  if ((tab = put_in_tab(av[1], tab)) == NULL)
    return (1);
    return (0);
 }

When I use I have this result 我得到这个结果

��

��

��

��

��

��

��

��

��

��

��

��

��

��

USER=benoit.pingris
JRE_HOME=/usr/lib64/jvm/!
LS_COLORS=no=00:fi=00:di!
LD_LIBRARY_PATH=:/home/b!
XDG_SESSION_PATH=/org/fr!
XNLSPATH=/usr/share/X11/!
GLADE_MODULE_PATH=:/usr/!
XDG_SEAT_PATH=/org/freed!
HOSTTYPE=x86_64
QEMU_AUDIO_DRV=pa
CPATH=:/home/benoit.ping!
SSH_AUTH_SOCK=/tmp/ssh-a!
SESSION_MANAGER=local/pc!
FROM_HEADER=
CONFIG_SITE=/usr/share/s!
PAGER=more
CSHEDIT=emacs
XDG_CONFIG_DIRS=/etc/xdg!
MINICOM=-c
on

As you can see this is isn't the result I expect to have. However if I decide to call my function without but a string like this 而是像这样的字符串来调用函数

my_str_to_wordtab("this is an orignal test", tab)

it works fine.

What I understand from your code is that you want to put a text into tab. Then you should not allocate tab itself, but set the pointer into tab as value.

So you are allocating this:

tab = malloc(...)

This should be something along the lines of

*tab = malloc(...)

Otherwise, the actual tab value you send in as parameter gets overridden, causing the destination to be wrong.

If you'd declare tab to be a single, unallocated, pointer, then you can send it in using the ampersand:

...
char *tab;
...
tab = NULL;
...
my_str_to_wordtab(..., &tab);

Also, I would advice to use a const parameter to your source string, to keep you from modifying it incorrectly.

Also, I do not think you want to do it this way, as without free() calls, it is a lot of memory leakage.

What you are getting on the screen is the side effect of reading invalid memory, and you are lucky to get anything, a page/segmentation fault would be likely to occur and this way security breaches are created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM