在双指针上使用strcpy时出现分段错误

时间:2017-09-09 00:33:32

标签: c pointers segmentation-fault double-pointer pointer-to-pointer

我对C很陌生,但在Java和Python上编写了很多东西。所以我得到了基础知识 - 然而,C不断地用这个Segmentation故障打我:11无论我如何绕着我的代码包裹我。我怀疑问题可能是我访问超出我的数组的界限,即在这一部分:

ordListe[i] = (char*)malloc(n);
strcpy(ordListe[i], ord);

几个小时我一直不知如何解决这个问题,现在我转向你:我错过了什么?我找不到很多涉及指针指针的帖子有类似的问题,所以我认为这对未来的其他人也会有所帮助。发生问题的全部功能:

char** split(char* s)
{
  char* setning = (char*) malloc(strlen(s) + 1);
  strcpy(setning, s);  //Setning blir det samme som s, saa vi slipper aa roere s
  printf("%s\n", setning);  //TNB: Feilsoeking
  char* ord = (char *) malloc(strlen(setning) * sizeof(char));  //Henter ut ordet
  ord = strtok(setning, " ");  //Henter foerste ord
  printf("%s", ord);
  int i = 0;
  size_t n = 0;
  char** ordListe = (char**) malloc(strlen(s) * sizeof(char));  //Lager en liste vi kan lagre verdiene i
  ordListe[strlen(setning)+1] = NULL;
  while(ord != NULL)
  {
    n = strlen(ordListe[i]) + 1;
    ordListe[i] = (char*)malloc(n);
    strcpy(ordListe[i], ord);
    i++;
    ord = strtok (NULL, " ");  //Beveger den videre
  }
  free(setning);
  return ordListe;
}

该函数的目的是拆分字符串并将各个部分存储在指向指针的数组中并返回所述数组。 提前谢谢!

1 个答案:

答案 0 :(得分:0)

这是一种方法:

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

/****************************************************************
**
*/
char **split(const char *s)
   {  
   char  *setning  = NULL;
   char  *ord      = NULL;
   char **ordListe = NULL;
   int    elements = 0;
   char **new;

   /* Build non-const version of the input string. */
   errno=0;
   setning = strdup(s);
   if(!setning)
      {
      fprintf(stderr, "strdup() reports: %d %s\n", errno, strerror(errno));
      goto CLEANUP;
      }

   /* Parse the string into an array of strings. */
   ord = strtok(setning, " "); 
   while(ord)
      {
      /* Increase the array of string pointers by one. */
      errno=0;
      new=realloc(ordListe, sizeof(char *) * (elements + 1));
      if(!new)
         {
         fprintf(stderr, "realloc() reports: %d %s\n", errno, strerror(errno));
         goto CLEANUP;
         }

      ordListe=new;

      /* Append an allocated string to the end of the array of strings. */
      ordListe[elements] = strdup(ord);
      if(!ordListe[elements])
         {
         fprintf(stderr, "strdup() reports: %d %s\n", errno, strerror(errno));
         goto CLEANUP;
         }

      ++elements;
      ord = strtok(NULL, " "); 
      }

   /* Add a NULL termination value to the end of the array of strings. */
   errno=0;
   new=realloc(ordListe, sizeof(char *) * (elements + 1));
   if(!new)
      {
      fprintf(stderr, "realloc() reports: %d %s\n", errno, strerror(errno));
      goto CLEANUP;
      }   

   ordListe=new;

   ordListe[elements] = NULL;

CLEANUP:

   /* Free the working copy of the string. */
   if(setning)
      free(setning);

   /* Free everything if there was an error, and return NULL. */
   if(errno)
     {
     while(elements)
        free(ordListe[elements--]);

     free(ordListe);
     ordListe=NULL;
     }

   return ordListe;
   }

/****************************************************************
** Program start.
*/
int main(int argC, char *argV[])
   {
   char **ordListe = NULL;
   char **iter = NULL;

   /* Split a string. */
   ordListe=split("Now is the time for all good men to come to the aid of their country.");
   if(!ordListe)
      {
      fprintf(stderr, "split() failed.\n");
      goto CLEANUP;
      }

   /* Print each split-out string. */
   iter = ordListe;
   while(*iter)
      {
      printf("%s\n", *iter);
      ++iter;
      }   

CLEANUP:

   /* Free the array of strings. */
   if(ordListe)
      {
      iter = ordListe;
      while(*iter)
         {
         free(*iter);
          ++iter;
         }

      free(ordListe);
      }

   return(0);
   }