473,503 Members | 1,910 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Doubt about array's name

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 */
Jun 27 '08 #1
5 1725
nembo kid said:
In the following function, [shouldn't s] be a pointer costant (array's
name)?
It's just a copy. C is pass-by-value. When the argument expression is
evaluated (in the *call* to chartobyte()), the array name is treated as if
it were a pointer to the first element in the array, and this pointer
value is copied into the parameter object that the implementation
constructs for the call.
So why it is legal its increment?
Why not? It's only a copy, after all. It doesn't affect the original array
address in any way whatsoever.
/* Code starts here */
void chartobyte (char *s) {

while (s!=0) {
printf ("%d", *s);
s++;
}
/* Code ends here */
I think you meant to write:

void chartobyte(const char *s) /* significant difference #1 */
{
while(*s != '\0') /* significant difference #2 */
{
printf("%d", *s);
s++;
}
}

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #2
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
array[1] == array++; *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, ..

#include <stdio.h>
#include <string.h>

int main() {
int i;
char *p_str = "mystring";
printf ("%s\n", p_str);
for (i = 0;i < strlen(p_str); i++)
printf ("%c", p_str[i]);
printf ("\n");
return 0;
}
Jun 27 '08 #3
Richard Heathfield ha scritto:
Why not? It's only a copy, after all. It doesn't affect the original array
address in any way whatsoever.
Ok my doubts cleared all.

But anyway I dont'understand because someone add the qualificator
"const" to string array passed to the function (so to avoid any change
of this pointer).

/* Code */
void chartobyte (const char *s)
/* Code */

while(*s != '\0') /* significant difference #2 */
Yes, was my mistake.

Thanks again and apologies for my bad english.
Jun 27 '08 #4
nembo kid <us**@localhost.comwrites:
Richard Heathfield ha scritto:
>Why not? It's only a copy, after all. It doesn't affect the original
array address in any way whatsoever.

Ok my doubts cleared all.

But anyway I dont'understand because someone add the qualificator
"const" to string array passed to the function (so to avoid any change
of this pointer).
/* Code */
void chartobyte (const char *s)
/* Code */
``const char *s'' declares s as a pointer to const char, not as a
const pointer to char. In other words, you're permitted to modify the
pointer (since it's just a local copy), but you're not permitted to
modify what it points to.

If you wanted to declare a const pointer to char (so the pointer
itself is read-only), you could write:

char *const s;

But function parameters, since they're purely local to the function,
are not typically declared as "const", since any modification won't
affect the caller anyway. You could declare a parameter as "const" if
you don't intend to modify it within the function.

--
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"
Jun 27 '08 #5
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"
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2027
by: praba kar | last post by:
Dear All, I have doubt regarding sorting. I have a list that list have another list (eg) list = ,,] I want to sort only numeric value having array field. How I need to do for that.
138
5104
by: ambika | last post by:
Hello, Am not very good with pointers in C,but I have a small doubt about the way these pointers work.. We all know that in an array say x,x is gonna point to the first element in that...
5
2151
by: chella mani | last post by:
hi all, I am developing a tool for scientific application which must be time and memory efficient,my tool uses 1D,2D and 3D arrays. Is it a good practice to use 3D arrays in applications or I...
9
4965
by: grid | last post by:
Hi, I have a function which takes a variable number of arguments.It then calls va_start macro to initialize the argument list.But here in my case I have a special builtin va_start provided by the...
1
1370
by: Solli Moreira Honorio | last post by:
Hi, I've a method that return a array, and my doubt is how i'll create a array variable that will receive the return if I don't know the size of return. There are another way to do this...
13
1678
by: sathyashrayan | last post by:
Dear group, pls go through the following function definition: function at_show_aux(parent, child) { var p = document.getElementById(parent); var c = document.getElementById(child); var top ...
5
2001
by: subramanian | last post by:
Consdier the following program: #include <stdio.h> struct typeRecord { int id; char str; } records = { {0, "zero"}, {1, "one"},
29
2078
by: swapna.annamaneni | last post by:
hi, i tried with this simple code int array,i; for(i=-1;i<=sizeof(array);i++) but the condition is failing always even array size is greater than zero. can u help me.............
4
1557
by: pavanponnapalli | last post by:
hi, Here is my Code as under: #! /usr/bin/perl -w use DBI; my %hash; my @arr; my @arr1; my @arr2;
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5592
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4685
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
397
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.