-1

I start of with a

memcpy(g->db_cmd,l->db.param_value.val,l->db.param_value.len);

which contains the value "function" however I want a null character to be appended like "function'\0'" I've tried a

memcpy(&g->db_cmd[l->db.param_value.len],0,1);

This crashes the program. I've tried memset also

 memset(&g->db_cmd[l->db.param_value.len],0,1);

This doesnt work. Any idea?

pgSystemTester
  • 8,979
  • 2
  • 23
  • 49
ken
  • 283
  • 2
  • 6
  • 12

6 Answers6

4
g->db_cmd[l->db.param_value.len] = 0;

assuming you have allocated space for that.

pyroscope
  • 4,120
  • 1
  • 18
  • 13
4

First off, C (and C++) is not dynamic like you know it from Java, C#, PHP and others. When you are presented with a string in C, the string is pretty much static in length.

To make the answer simpler, lets redefine your variables:

  • g->db_cmd will be called dest,
  • l->db.param_value.val will be called src, and
  • l->db.param_value.len will be called len.

You should allocate a new string of size len plus one (for the extra null).

Allocate a new dest:

dest = calloc(sizeof(char), len + 1);

calloc allocates an array of chars as long as len plus one. After calloc() has allocated the array it fills it with nulls (or \0) thus you automatically will have a null appended to your deststring.

Next, copy the src to dest with strncpy:

 strncpy(dest, src, len);

To convert this back to your variable names:

g->db_cmd = calloc(sizeof(char), l->db.param_value.len + 1);
strncpy(g->db_cmd, l->db.param_value.val, l->db.param_value.len);
Sean
  • 3,002
  • 1
  • 26
  • 32
Martin Olsen
  • 1,895
  • 1
  • 17
  • 20
  • In my opinion, `memcpy()` would be a more appropriate function to use than `strncpy()` in this example. Unless it's possible that the length of `l->db.param_value.val` is actually less than `l->db.param_value.len`. In which case, that's a confusing set of data and/or member names. – Michael Burr Aug 04 '11 at 21:39
  • Michael: you are absolutely right, given there is an Len variable. That would probably also shave of a few cycles when not checking for null in src. And Ken mentions elsewhere that he has to use memcpy(), so I should probably just change it! – Martin Olsen Aug 04 '11 at 23:00
3

If you want string-copying semantics, why not use a string-copying function?

ildjarn
  • 62,044
  • 9
  • 127
  • 211
  • cant use it i need to use memcpy – ken Aug 04 '11 at 20:32
  • 4
    @ken : *Why* though? If this is homework, then please tag your question as such. – ildjarn Aug 04 '11 at 20:33
  • 1
    Who says the source value is NUL terminated, esp. when it's a struct with a len attribute? – pyroscope Aug 04 '11 at 20:38
  • @pyroscope : Indeed, nobody said that. Vague questions sometimes yield incorrect answers -- that's just how it goes. – ildjarn Aug 04 '11 at 20:39
  • 3
    In this case it might be reasonable to assume the source and destination are null terminated (or should be) since another question by the OP using similar code uses `strlen()` all over the place: http://stackoverflow.com/questions/6931193/concatenate-with-memcpy However, maybe that's the problem the OP was having in that question... I think the OP needs to figure out what the data they're dealing with is supposed to be. – Michael Burr Aug 04 '11 at 21:32
0

Strings are by default null-terminated.
If you want a to ad an extra NULL at the end you can write "String\0"
or db_cmd[len]='\0'

cprogrammer
  • 5,503
  • 3
  • 36
  • 56
0

If the source you're copying from also contains a NULL terminated string use

memcpy( g->db_cmd, l->db.param_value.val, l->db.param_value.len + 1 );

Otherwise you'll have to add the terminator yourself.

g->db_cmd[l->db.param_value.len] = '\0';

Of course, you need to ensure that the destination has enough room for this character.

Praetorian
  • 106,671
  • 19
  • 240
  • 328
  • Hey, good point the value i am copying does contain it but even with the +1 it does not append ?? – ken Aug 04 '11 at 21:02
0

memcpy takes two pointers, and an integer. In the lines that you say are crashing, you pass it a pointer, and two integers. The code cannot dereference the second argument (0).
If you really really want to use memcpy, you have to have a pointer to a zero

char zero = 0;
memcpy(&g->db_cmd[l->db.param_value.len], &zero , 1);

But I would really suggest pyroscope's answer. It's faster, and clearer.

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158