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

时间:2022-09-06 18:42:37

I'm pretty new to C, but have programmed a great deal in both Java and Python. So I got the basics going – however, C keeps hitting me with this Segmentation fault: 11 no matter how I wrap my head around my code. I suspect the problem may be that I'm accessing beyond the bounds of my array, namely in this part:

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

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

For hours I've been at a loss how to solve this, so now I turn to you: what am I missing? I couldn't find many posts involving pointers to pointers out there with a similar problem, so i assume this will be of some help to others in the future too. Full function where the problem occurs:

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

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;
}

The purpose of the function is to split a string and store the individual parts in a pointer-to-pointer array and return said array. Thanks in advance!

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

1 个解决方案

#1


0  

Here is one way to do it:

这是一种方法:

#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);
   }

#1


0  

Here is one way to do it:

这是一种方法:

#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);
   }