1

So today I was messing around with some string parsing in C and I started receiving an odd string formatting. I believe it has something to do with the strcat between the array "functionName" and the pointer "fiterator", but I cant figure out how to fix it:

I'm trying to print "test()" but rather i'm receiving test()est()st()t()

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

void __INTERFunc(char args[])
{
  char * argsp;
  char functionName[25] = {};
  for (argsp = args; *argsp != '\0'; argsp++)
  {
    if (*argsp == '(')
    {
      char *fiterator;
      for (fiterator = args; *fiterator != '('; fiterator++)
      {
        strcat(functionName, fiterator);
      }
    }
  }
  char * nmiter = functionName;
  while (*nmiter != NULL)
  {
    printf("%c", *nmiter);
    nmiter++;
  }

  // This block is for testing purposes
  // Still receiving output: test()est()st()t()
  printf("\n");
  printf("%s\n", functionName);


}

int main()
{
  __INTERFunc("test()");
}
7
  • 1
    I'm confused. What is your function suppopsed to do? Is your function supposed to print test or test()? Anyway, I think the strcat is the source of the strange behavior but its hard to give an answer without knowing exactly what you want. The best thing I can suggest is that you should fill your programs with printf statements showing the state of the functionName buffer throughout the execution of the algorithm. That should clear things up a lot. Commented Oct 22, 2015 at 1:43
  • It should be printing test() but yeah I'll definitely try the printf statments Commented Oct 22, 2015 at 1:45
  • I understood that its supposed to print test() but what is the test() supposed to mean? Why can't you just printf("%s", args) and be done with it? Commented Oct 22, 2015 at 1:46
  • Because the point of the function is to analyze the text passed in and perform some block of code depending on what it finds is in that text. Commented Oct 22, 2015 at 1:51
  • OK. Are you familiar with lexing and parsing, btw? Its a more robust way to build interpreters than just doing ad-hoc string munging. Commented Oct 22, 2015 at 1:53

1 Answer 1

1

Function strcat concatenates strings. At strcat(functionName, fiterator); you're concatenating the string fiterator to the string functionName, but what you want is to append the pointed char to functionName.

At the first iteration, fiterator is pointing to the first letter, so it is equal to the string test(), which you concatenate to the empty string. At the second iteration, fiterator is pointing to the second letter and it just really is the string est(). When both strings get joined, that is when you get test()est().

Also, your iterator stops at ( so you will never get test(), only test.

Try something like functionName[i++] = *fiterator, where i is an integer for the char position, starting in 0.

  int i = 0;
  char functionName[25] = {};
  for (argsp = args; *argsp != '\0'; argsp++)
  {
    if (*argsp == '(')
    {
      char *fiterator;
      for (fiterator = args; *fiterator != '('; fiterator++)
      {
        functionName[i++] = *fiterator;
      }
    }
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.