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.
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”.
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.
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.
-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}
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.
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").
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.