hi,
I have a problem of concatenating 2 strings. 1 of the strings is defined in the code and the other one is asked from user. This small code must only concatenate these to strings but doesn't work...
here is my code:

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

struct deneme
{
        char *filename;
} *dene;

int main ()
{
        dene = malloc (sizeof(struct deneme));
        dene->filename = (char*)malloc(200*sizeof(char));
        char *path=(char*)malloc(sizeof(char)*250);
        path = "/bin/";
        printf("Enter a file name: ");
        fgets(dene->filename,200,stdin);
        printf("filename:%s",dene->filename);
        strcat(path,dene->filename);
        printf("concatenated: %s\n",path);

        return 0;
}

>dene = malloc (sizeof(struct deneme));
It's best to avoid explicit types wherever you can. In this case, you can take the sizeof *dene to get the same effect as sizeof(struct deneme).

>dene->filename = (char*)malloc(200*sizeof(char));
sizeof(char) is guaranteed to be 1, so it's superfluous. It's also a bad idea to cast malloc because it hides legitimate errors like forgetting to include stdlib.h, or compiling as C++ when you really wanted to compile as C.

>char *path=(char*)malloc(sizeof(char)*250);
This tells me that you're trying to compile as C++. C doesn't allow declarations after non-declaration statements in a block. Well, it does with the latest standard, but it's not well implemented yet, and if you know how to compile as C99, you probably know enough to figure out the problems with this program.

>path = "/bin/";
This is the problem. You just caused a memory leak, and later you'll try to concatenate to a string literal, which is a big no-no.

Compare your code with this:

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

struct deneme
{
  char *filename;
} *dene;

int main ()
{
  char *path = malloc(250);

  dene = malloc(sizeof *dene);
  dene->filename = malloc(200);

  printf("Enter a file name: ");
  fgets(dene->filename,200,stdin);
  printf("filename: %s\n",dene->filename);

  strcpy(path, "/bin/");
  strcat(path,dene->filename);
  printf("concatenated: %s\n",path);

  return 0;
}

It's also wise to make sure that malloc succeeded, and free the memory you allocated when you're done with it.

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

struct deneme
{
  char *filename;
} *dene;

int main ()
{
  char *path = malloc(250);

  if (path != NULL) {
    dene = malloc(sizeof *dene);

    if (dene != NULL) {
      dene->filename = malloc(200);

      if (dene->filename != NULL) {
        printf("Enter a file name: ");
        fgets(dene->filename,200,stdin);
        printf("filename: %s\n",dene->filename);

        strcpy(path, "/bin/");
        strcat(path,dene->filename);
        printf("concatenated: %s\n",path);

        free(dene->filename);
      }

      free(dene);
    }

    free(path);
  }

  return 0;
}

Thanks for your help,
i have one more question! Now it is working all right, but when i give it to the execv command as

execv(path,NULL,0)

it does not do the operation it should do... When i write

execv("/bin/dmesg",NULL,0)

it works well but when i use path it does not... (By the way the user enter dmesg of course. I'm just testing, i know that some commands have some arguments and i cannot simply wirte NULL for the second argument. I just try to figure out why execv does not working with my path variable??) :rolleyes:

I found my wrong... This is because my path variable contains a new line at the end. When i get rid of that new line it worked :cheesy: Thanks again...

>This is because my path variable contains a new line at the end.
Yep, fgets copies the newline to the array. If you don't want it, you need to remove it:

{
  char *newline = strchr(dene->filename, '\n');
  if (newline != NULL)
    *newline = '\0';
}
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.