Here is a simple substring type function that I wrote in C.

char *substr(char arr[], int pos1, int pos2)
{ 
  char str[100];
  char *sub;
  
  for (int i = 0; i <= (pos2 - pos1); i++)
  {
    str[i] = arr[i+pos1];
  }
  
  sub = str;
  printf("Sub: %s\n", sub);  // debugging purposes
  
  return sub;
}

Here is my function call in main.

int main(int argc, char *argv[])
{
  char str[] = "Programming in C!";
  char *sub = substr(str, 0, 1);

  printf("String: %s", sub);
  return 0;
}

And here are my commands and output.

% cc -v -o search searchstr2.c
% search

Sub: Prúü
String:

Here's what I want the output to be is:

Sub: Pr
String: Pr

I can't figure this out and it's driving me bonkers. Any help would be greatly appreciated.

Kind Regards,
--
Jay

Recommended Answers

All 3 Replies

Your substr function returns a pointer to its local variable str. This automatic storage variable are discarded immediatly after return, so this pointer value is senseless one. The classical novice's error ;)
The second defect: you forgot to append terminated null byte at the end of the formed substring.
The 3rd defect: no parameters check at all (positions >= 0, pos1 <= pos2 etc) .
Of course, you can declare str as a static variable (bad solution: every substr call reuses the same static storage), or you can add yet another pointer parameter for extracted substring...

Hi,

Thanks for the quick reply and wake up call! "I see said the blind man" :). That actually makes sense. I made some changes and all seems well now. I'll add some error checking and comments later as this is only a very very small piece of a humongous program. Please let me know if you (or anyone else) see anything else that may be harmful as I'm sure there is.

Thanks again for making my first post a great experience.

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

char *substr(char* dest, const char* str, int startpos, int endpos)
{ 
  /* error checking will be added */

  for (int i = 0; i <= (endpos - startpos); i++)
  {
    dest[i] = str[i + startpos];
    dest[i + 1] = '\0';
  }

  printf("Sub: %s\n", dest); /* debugging purposes */
  
  return dest;
}

int main(int argc, char *argv[])
{
  char *str = "Programming in C!";
  char *sub = malloc(strlen(str));
  int p1 = 0, p2;

  printf("String: %s\n", substr(sub, str, 0, 1));

  return 0;
}

Kind Regards,
--
Jay

No need in dest[i + 1] = '\0'; in the loop:

int i = 0, n = (endpos-startpos)+1;
if (dest && str && startpos >= 0) { // prevent future troubles!
    if (startpos < strlen(str) && n > 0) {
        str += startpos;
        for (i = 0; i < n && str[i]; ++i) /* break if str ended! */
            dest[i] = str[i];
    }
    dest[i] = '\0';
}

No need in printf "debug" statement in substr body.
No need in dynamic allocated memory in main (and you forgot to add 1 for null terminator).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.