Ofloo <in**@ofloo.netwrites:
On 30 apr, 15:34, nembo kid <u...@localhost.comwrote:
>In the following function, s shouldn't be a pointer costant (array's name)?
So why it is legal its increment? Thanks in advance.
/* Code starts here */
void chartobyte (char *s) {
while (s!=0) {
printf ("%d", *s);
s++;}
/* Code ends here */
From what i understood from arrays is that array[0] == *array and
Yes, this follows from the definition of the [] operator.
arr[i] means *(arr+i)
This works because arr is an array expression and is therefore
converted, in most but not all contexts, to a pointer to its first
element.
array[1] == array++;
No. array[1] is equivalent to *(array+1). array++ is (a) of the
wrong type, and (b) illegal, since ``array'' is not a modifiable
lvalue.
I've seen a tendency among some new C programmers to over-use the "++"
operator. "x++" is not just a cool way to say "x + 1". It yields the
original value of x and, as a side effect, modifies x.
I think what you meant to say is that array[1] == array+1 -- but
that's still incorrect, because array[1] is an element of the array,
and array+1 is a *pointer* to that element.
*array .. and so forth if the predefined size of
the pointer is 4bytes array++ will go to the next 4bytes in memory ..
basicly pointers are arrays and arrays are pointers, .. the other
thing should work also the other way arround, ..
That is one of the most common fundamental misunderstandings about C.
Arrays are not pointers. Pointers are not arrays. Most operations on
arrays are defined in terms of pointers, but they are still two
entirely distinct things. It sometimes seems as if the language is
designed to obscure this important distinction.
The comp.lang.c FAQ is at <http://www.c-faq.com/>. Read section 6,
"Arrays and Pointers".
The following code is basically sound, but I'm going to critique some
of the details anyway.
#include <stdio.h>
#include <string.h>
int main() {
Ok, but "int main(void)" is better.
int i;
char *p_str = "mystring";
It's better to add a 'const' keyword here. You're not allowed to
modify the contents of a string literal; using 'const' makes this
explicit.
printf ("%s\n", p_str);
for (i = 0;i < strlen(p_str); i++)
strlen() returns a result of type size_t. Though you certainly can
store that result in an int, it's usually better to keep types
consistent. In this case, for traversing a string, using int can't
cause any actual problems unless the string is longer than 32767
characters. But it's a good habit anyway.
Note that size_t is an unsigned type, which can make using it a trifle
more complicated. For example, if i is of type size_t, then a
comparison like ``i >= 0'' (if you're counting down rather than up)
won't work; an unsigned type is *always* >= 0.
A more serious problem here is performance. strlen() has to scan from
the beginning of the string to the terminating '\0' to determine its
length; in computer science terms, it's an O(N) operation, where N is
the length of the string. You evaluate strlen(p_str) every time
through the loop (even though the length never changes), so the loop
as a whole is O(N**2) (N squared). The effect isn't noticeable in a
small program like this (more to the point, with a short string like
"mystring"), but it can be serious in larger programs.
Save the value of strlen(p_str) in a variable, and use that variable
in the loop.
printf ("%c", p_str[i]);
printf ("\n");
return 0;
}
--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"