Strncpy

Now getting this error:

error: invalid operands to binary expression
('int' and 'char *')
* ++dest = c;
^ ~~~~~~


Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

#include <stdio.h>
#undef strncpy


char *STRNCPY(char *dest, const char *src, int n)
{
	
	char c;
	char *s = (char*)src;
	
	do
	  {
	      c = *s++
		  *++dest = c;
		  if (--n == 0)
		  return dest;
	  }
	  while (c != 0);
	  
	  
	  do
	    {
			*s++ = 0;
			--n;
	    }
		while (n > 0);
	  
		return dest;
}




Where could this error come from?
Last edited on
What is the precedence of the operators on that line? In which order are they evaluated?
There's a missing semicolon on line 14.
To make it compilable, put a semicolon at the end of line 14. c = *s++ ;

The logic is broken;
see: http://www.opensource.apple.com/source/Libc/Libc-498.1.7/string/FreeBSD/strncpy.c
Ok... How would I test this functions performance?
But the strategy is definitely: first make it work, then make it right, and, finally, make it fast.
- Stephen C. Johnson and Brian W. Kernighan
in 'The C Language and Models for Systems Programming', Byte magazine (August 1983)

https://archive.org/details/byte-magazine-1983-08
Topic archived. No new replies allowed.