How do I point a char pointer to a part of another char array?
Programming in C.
I have two char arrays. char buf1[1024]; char buf2[1024]; Aren't buf1 and buf2 also pointers? I read in 1024 bytes into buf1 which contain about 300 bytes of characters with newlines. The data is basically a few English sentences. I'm trying to scan buf1 for newlines and then stop at the 1st newline and copy the rest of the data from that 1st newline into buf2. So I run a for loop to look for that new line. Code:
for(i=0; i<1024;i++) { However I'm getting the error 'incompatible types of assignment' for the line buf2 = buf1[i]; Could somebody explain this process better. On the above statement buf2 is a pointer and buf1[i] is a string? a char? Please be as detailed as possible. I really want to understand this. |
Ok, I think you're getting some things jumbled.
You declare buf1 and buf2 as: Code:
char buf1[1024]; You need to distinguish the underlying data types from what the data type represents. I say that because I think you're getting confused by thinking that character arrays, character pointers, and strings are all synonymous--they aren't. A string is an interpretation of underlying data. The compiler looks at a starting memory address, and "displays" each byte as a character until it reaches a byte with a value of 0 (the end-of-string marker). A character pointer is a variable that stores a memory address, and indicates that the memory it's addressing contains characters. But the pointer's value (the address itself) can be modified. A character array is a variable fixed at one memory address, with enough space available to store a fixed number of characters. For lack of a better analogy, think of it as a constant character pointer--the address cannot be modified. Now, you can use both a character pointer and a character array whenever you need to supply a string to your code. And I think that might be part of what's tripping you up. A character array, with no subscript, is a pointer to the first element in the array. When you apply a subscript to an array, you're left with the character value located at that index in the array. With that out of the way... Your statement: Code:
buf2 = buf1[i+1]; Second, buf2 is a character array, and its location in memory cannot be modified. Therefore, any assignments to buf2 (without an appropriate subscript) will also result in an error. I assume what you want is for buf2 to contain the same characters as buf1, from a given starting position. You should use the strcpy() or strncpy() functions for that purpose. You will need to use the address-of operator (&) that you touched on in your post to make this work correctly. For example: Code:
strcpy( buf2, &buf1[i+1] ) |
I seem to keep pointing people to this, but it really is easy to follow:
http://www.highercomputingforeveryone.com/ Carl takes you through the basics in very small steps, Regarding your immediate problem, you really shouldn't copy a buffer one character at a time, this will perform badly in terms of compute cycles. Try to come up with a solution that performs a string copy for example, or maybe you could just not allocate buff2 - create and initialise it pointing at [buff1+i] hth |
Quote:
Code:
if((strcmp(&buf1[i], "\n")).. Code:
if((strcmp(buf1[i], "\n")).. 'Hope that helps |
I don't understand why when you use strcmp()
why wouldn't you use buf1[i] which is a char to compare it with "\n" which is another char? Why is &buf1[i] which is a memory address where the char in the ith index is stored correct when comparing to another char "\n"? |
Quote:
First, strcmp() compares strings. By comparing strings, the process necessarily compares individual characters, but strcmp() will continue to compare characters including and until a NULL is encountered. So, for your first question: Quote:
2. "\n" is NOT a character. It is a literal string. It is represented in memory as two sequential characters at the following indexes: [0]: \n [1]: NULL. Use of "\n" in code is replaced with the address in memory where the literal string is stored. Please note: "\n" is a literal string -- '\n' is a literal character. The quoting makes a difference. If you want to compare two characters individually, you would do it like so: Code:
if( buf1[i] == '\n' ) Quote:
But your hangup I believe, is again thinking that "\n" is a character. As mentioned above, it is actually a literal string. It represents a sequence of two characters--not one. |
Hi, again:
Quote:
Code:
// Compare string "\n" with string "\n": PS: Q: does this make sense? Quote:
|
Quote:
Code:
int count = 5; Similarly, Code:
char *pnt; Quote:
buf1[i] is a character, not an address, so passing it as an address to strcmp would seg fault (as many people already explained). &buf1[i] is the address of a substring that might start with '\n' (which is what you said you want to test) but strcmp would test whether it both starts and ends with '\n', which is not what you want. So instead of strcmp(&buf1[i], "\n")==0 what you want is buf1[i]=='\n' Quote:
"\n" is the address where that char followed by a null is stored. '\n' is the char. strcmp compares two sequences of characters, and you must pass it their addresses. To compare individual characters, you just use == as I suggested above. For the other error... Quote:
Quote:
Code:
if ( buf1[i] == '\n' ) { |
Thanks a ton guys, that really cleared it up.
|
All times are GMT -5. The time now is 05:45 PM. |