Am i not doing somthing corect with strncpy

so i have two strings. and i want to copy just a part or one into the other.

char buf[40]= “This is the test string”;

unsinged char a,x;

struct the_http_request

{

char method[10];

char type;

char filename[40];

}http_request;

a=4;

x=6;

strncpy(http_request.filename,&buf[a],x);

printf(“%s”,http_request.filename);

now, the wway i understand it it should copy the text “is the” into http_request.filename, but when it prints i allways get a weird character at the end.

Check out this page - http://www.openbsd.org/cgi-bin/man.cgi? … &sektion=3.

char * strncpy(char *dst, const char *src, size_t len);

strncpy() copies not more than len characters into dst, appending `\0’ characters if src is less than len characters long, and not terminating dst if the length of src is greater than or equal to len.

From the looks of things, the length of your src array is >= your len, so it is not NULL-terminating the dst string. Therefore you need to put a NULL terminator (‘\0’) at the end of the buffer for it to print out correctly using printf(). Also, character 4 of your array is actually the space between “This” and “is” (at least using standard C notation). You should change the variable a to 5 to start at the “i” in “is”.

Therefore you need to put a NULL terminator (‘\0’) at the end of the buffer for it to print out correctly using printf().

Thats what i though so i added :

strcat(http_request.filename,“\0”);

after the strncpy function. but then i still get the extra caracter. it just prints out corectly. I just am trying to avoid doing this in a loop.

Why not just do this:

http_request.filename[x] = '\0';

i just thought strcat would work, but honestly i didn’t know you could access the data like that as well. i admittedly suck at pointers :roll:

i have a few C books and it seems the shortest section is the pointers.

i allways see people doing things that are not in the books and i wonder where do they pick up that $hit at. I learn by example, and most of the books tell me how to use it, quite honestly i need to see how to use it.

sounds the same but its different.

btw, thanks for the help, i tried http_request.filename = ‘\0’; and it works just fine now.

Thanks again

Just remember that in C all strings are null terminated arrays. A null is ‘\0’.

so if you declare

char test=“Hello”

you have defined an array containing

test[0]=‘H’;

test[1]=‘e’;

test[2]=‘l’;

test[3]=‘l’;

test[4]=‘0’;

test[5]=‘\0’;

C uses the null to understand when a string ends.

All C functions to handle string work only if the string is null terminated, that’s why you can’t use strcat.

Since you can’t use C’s string handling functions on a non null-terminated string, you need to access it like an array.

yes, your example i have understood.

the things that confuse me are stuff like why are string defined as char, and not uchar ? there will never be a “-” value ASCII character.

the other thing that is confusing to me is say:

char *string1;

char string2[80];

char *useless;

strcpy(string1,string2);

how does the memory get managed here, i mean string2[80] is specifically saying i want 80 bytes, but if i use string1 and put 30 bytes in there, then later i write to useless, how does it keep from writing into string1. i know i explained this bad, but i beleive you get what i am saying.

First I correct a mistake in my previous post:

char test=“Hello”;// is wrong

char test=“Hello”;// is correct

Ok, going on, if you declare

char test[]="Hello";
char copy[80];

-test will be an array of 6 char (5+‘\0’). if you declare it with the the compiler automatically fills in the correct size.

This will forever be a string capable of holding no more than 5 chars.

if you copy a shorter string into it, then the ‘\0’ will move to the end of the actual string. All bytes after the null will continue to exist and contain garbage.

-copy is a string capable of holding 79 char (since the ‘\0’ must be present @ the end). If you do like this

strcpy(copy,test);

the string copy will continue to take up 80 bytes of ram, but will be 5 bytes of length.

Its content will look like this {‘H’,‘e’,‘l’,‘l’,‘o’,‘\0’, garbage till 80th byte}

That’s how memory is managed

ok, i understnad all that, but what happens in the case when you use “char *string_data” and copy to that how is memory managed.

Ah, and strings are usually declared as char instead of unsigned char because ASCII characters are in the range 32 to 127.

a signed char has a range of -128 to +127

an unsigned char has a range of 0 to 255

so the ASCII character set fits into both types.

ok, i understnad all that, but what happens in the case when you use “char *string_data” and copy to that how is memory managed.

If you mean something like this

char *test="Help";

it’s just an alternate way of defining this

char test[]="Help";

and your code will work fine because it’s correct.

But if you do something like this

char *test;
char str[]="Hello";
strcpy(test,str);

The program will crash, or write to random memory location, if I’m correct:

you’ve defined a pointer to a string, that is capable of pointing, but has no memory on its own to store data.

In this case you need to allocate some memory to the pointer:

char *test;
char str[]="Hello";
test=(char *)malloc(50*sizeof(char));//Allocating memory for a 49 char string
strcpy(test,str);

ok, thats about everything i know so far.

When i see somthing like this is where what you just said falls apart on me. becase there is no malloc used. so how does it work.

struct msg {

char *p1;

char *p2;

} myptrs;

myptrs.p1 = “Hello World”;

myptrs.ps = “Goodby World”;

does this fall into the situation as :

char temp = “Hello World”;

whereas, once compiled it will allways have 12 memory locations for it.

Well, the only solution I can think is that happens something like this:

if you write something like this

char aaa[]="Hello";
char *bbb;
bbb=aaa;

This way bbb points to aaa. This does NOT mean that they are 2 separate strings allocated to different memory locations, BUT that you now have a string that is accessible in 2 way, from aaa and from bbb; If you modify aaa and read bbb you notice that also bbb has changed because the are the same string.

I suppose that in the case you posted, the compiler allocates place for a string without a name, and then makes it accessible also from the pointer. Since the string doesn’t have a name it’s accessible only from pointer.

Anyway I doubt this will work on all compilers. I never write code that way, and never come across a program coded in that way.

struct msg { 
char *p1; 
char *p2; 
} myptrs; 

myptrs.p1 = "Hello World"; 
myptrs.ps = "Goodby World";

Most likely, this will work as long as all of the above statements are global. If you try to assign myptrs.p1 in a function somewhere, then there is most likely an implicit call to malloc() (or some other memory-allocating function) that the compiler will generate. If you don’t have malloc() available to you in the link phase, then the linker will probalby complain (I have done this before).

inventore123:
This way bbb points to aaa. This does NOT mean that they are 2 separate strings allocated to different memory locations, BUT that you now have a string that is accessible in 2 way, from aaa and from bbb; If you modify aaa and read bbb you notice that also bbb has changed because the are the same string.

I suppose that in the case you posted, the compiler allocates place for a string without a name, and then makes it accessible also from the pointer. Since the string doesn’t have a name it’s accessible only from pointer.

This is known as a "shallow copy." Basically you are just copying the address that aaa points to to bbb, not the actual data held in thise locations. This is the main purpose in the strcpy and strncpy functions, in that they will copy all of the data from one string to another (also known as, you guessed it, a "deep copy").

thanks, how about this one.

static void NewFunction(void)

{}

what does it mean to put static in front of the void.

Both books i have dont explain or even show this.

maybe its time for new books.

static in front of a function limits the scope of that function to that specific source file. This means that the function cannot be accessed from other files. In some cases this is a good thing, because it even allows you to use the same function name in different files.

In general it is a good idea to limit the scope of functions and variables. I think lint will even produce a warning/info message to suggest to make the function static for functions that are only used locally in a source file.

static in front of a function has a completely different meaning than static in front of a variable.

Thanks