Jonhhan
Jonhhan

Reputation: 13

double pointer c memory fault

Basically I need to write a split function and currently I need to know how to populate substrings with pointers to characters from s.

I have:

char *s = "--ab--c--";
char **substrings;
int split(char *s, int start, char sep, char **substrings, int max)

I don't know if definition for *s and **substrings are correct.

And I need to assign pointers to *s such as **substrings would contain:

{ "", "ab", "c", "" }

Formal definition of substrings

substrings - the array to populate with pointers to substrings of s

I don't know, I googled double pointed and couldn't figure out.

The size of *s is unknown and the number of **substrings is only know when program is executed.

I am new to C, but I think I need something like this:

substrings[0][0] = "";
substrings[1][0] = "a";
substrings[1][1] = "c";
substrings[2][0] = "c";
substrings[3][0] = "a";

Upvotes: 0

Views: 112

Answers (3)

jcm
jcm

Reputation: 2578

As you don't know number of substrings or size of every substring in advance, I would suggest to use calloc taking into account worst case, this is:

substrings = calloc(strlen(s), strlen(s)*sizeof(char)); 

sizeof(char) should be 1 but I included it just for didactic reasons.

About best way to implement split function, I think the best solution would be using strchr, it would be something similar to:

int split(char *s, int start, char sep, char **substrings, int max)
{
    char *old, *sp;
    int i=0;
    old=s;

    sp=strchr(old,sep);    
    while (sp!=NULL && i < max)
    {
        if(0 < (sp-old))
            strncpy(substrings+i++, old, sp - old);
        old = sp+1;
        sp=strchr(old,sep);
    }

    // Last sub string
    if(0 < (s+strlen(s)-old))
        strncpy(substrings+i++, old, s+strlen(s)-old);

    return i;
}

For your proposed input, this solution would dump an array containing: {"ab","c"}.

I assumed that max defines maximum allowed number of substrings and that every element in substrings has space enough to store corresponding substring (both conditions fulfilled with previously proposed calloc).

Upvotes: 0

kronion
kronion

Reputation: 711

At runtime, you know that you cannot have more substrings than you have characters in s. Thus, you can calloc enough space for that many substrings and only use the space you need.

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

  char** substrings = (char**)calloc(strlen(s), sizeof(char));
  if (substrings == NULL) exit(1);

  // Split here

At this point, you have a data structure which holds pointers for strlen(s) strings. When you split your string, you will iterate through these pointers and assign each pointer to the new substring you have found.

Upvotes: 0

Alexander L. Belikoff
Alexander L. Belikoff

Reputation: 5711

It is unclear what the semantics your split() routine is but I 'm guessing that your substrings should be an array of pointers:

#define MAX_TOKENS  16 /* for example */
char* substrings[MAX_TOKENS];

split(s, ..., substrings, MAX_TOKENS);

Upvotes: 1

Related Questions