I'm trying to write a simple program that takes arguments and concatenates them into a single string.

It compiles fine, but when I run it, I get a segmentation fault.

Here's the code:

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

main(int argc, char *argv[])
{
  int i;
  int strlen;
  char str[100];
  *str = 0;
  for (i = 1; i <= argc; i++)
  {
        strcat(str,argv[i]);
  }

  printf("%s\n", str);
  return 0;

}

I can't figure out what I'm missing.

Thanks.

Recommended Answers

All 2 Replies

argv[argc] is a null pointer. Your loop runs too far by one.

That was it.

Thank you.

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.