In trying to understand the issue, I wrote this;
#include <stdio.h>
void f_output(char arg1[6], int limit);
int main () {
f();
return 0;
}
void f(void) {
char matrix[2][6] ={ {'h','e','l','l','o','\0'},
{'w','o','r','l','d','\0'}};
f_output(matrix, 10);
}
void f_output(char *s, int limit) {
int x, i;
for (i=0; i < limit; i++)
printf("%s\n", s++);
}
Desired output: "Hello world"
Actual output:
hello
ello
llo
lo
o
world
orld
rld
ld
My naive way of looking at this was to assume that the declaration
"void f_output(char arg1[6], int limit)"
would "tell" the pointer that the type was an array of 6 chars, hence s
++ would increment to matrix[2]. I understand this is a totally
useless and pointless function, except in trying to further my
understanding...so please go easy!!! :-) 17 2243
In article <11**********************@b75g2000hsg.googlegroups .com>,
mdh <md**@comcast.netwrote:
>In trying to understand the issue, I wrote this;
>void f_output(char arg1[6], int limit);
>void f_output(char *s, int limit) { int x, i; for (i=0; i < limit; i++)
printf("%s\n", s++);
}
You have a disagreement between the function declaration
and the function definition. In one place you used char arg1[6]
and in the other place you used char *s -- array vs pointer.
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
mdh wrote:
In trying to understand the issue, I wrote this;
#include <stdio.h>
void f_output(char arg1[6], int limit);
int main () {
f();
Where's the prototype for f()?
return 0;
}
void f(void) {
char matrix[2][6] ={ {'h','e','l','l','o','\0'},
{'w','o','r','l','d','\0'}};
f_output(matrix, 10);
}
void f_output(char *s, int limit) {
Head the warning your compiler should have given you here - this doesn't
match the prototype.
--
Ian Collins.
Ian Collins wrote On 04/11/07 17:37,:
mdh wrote:
>>In trying to understand the issue, I wrote this;
#include <stdio.h> void f_output(char arg1[6], int limit); [...]
void f_output(char *s, int limit) {
Head the warning your compiler should have given you here - this doesn't
match the prototype.
As far as I can see, the definition and declaration
match perfectly. Yet both you and Walter Roberson say
they disagree ... In light of 6.3.5.7p7
A declaration of a parameter as "array of /type/"
shall be adjusted to "qualified pointer to /type/"
[...]
.... could one or both of you explain the disagreement?
To the O.P.: Questions 6.21 and 6.4 in the comp.lang.c
Frequently Asked Questions (FAQ) at http://www.c-faq.com/
may help your understanding.
-- Er*********@sun.com
"mdh" <md**@comcast.netwrote in message
news:11**********************@b75g2000hsg.googlegr oups.com...
In trying to understand the issue, I wrote this;
#include <stdio.h>
void f_output(char arg1[6], int limit);
int main () {
f();
return 0;
}
void f(void) {
char matrix[2][6] ={ {'h','e','l','l','o','\0'},
{'w','o','r','l','d','\0'}};
What's wrong with
char matrix[2][6] ={ "hello", "world" };
?
f_output(matrix, 10);
If you want f_output to output the entire matrix, it should be declared as
(also improving the names a bit)
void f_output(char string_list[][6], int count);
and called with
f_output(matrix, 2);
}
void f_output(char *s, int limit) {
int x, i;
for (i=0; i < limit; i++)
printf("%s\n", s++);
}
With the above declaration,
void f_output(char string_list[][6], int count)
{
int i;
for (i = 0; i < count; i++) {
printf("%s\n", (char*)string_list++);
}
}
Of course, a more normal way to achieve this would be
printf("%s\n", string_list[i]);
>
My naive way of looking at this was to assume that the declaration
"void f_output(char arg1[6], int limit)"
would "tell" the pointer that the type was an array of 6 chars, hence s
++ would increment to matrix[2]. I understand this is a totally
useless and pointless function, except in trying to further my
understanding...so please go easy!!! :-)
With the argument char arg[6], the increment "arg++" would work on the
pointer to char value of the address to the first element in the array arg,
and arg will point to the next char in the array.
With the argument char arg[][6], the increment "arg++" would work on the
pointer to char[6] value of the address to the first element in the array
arg, and arg will point to the next char array in the array of arrays.
--
Jonas
Walter Roberson wrote:
In article <11**********************@b75g2000hsg.googlegroups .com>,
mdh <md**@comcast.netwrote:
In trying to understand the issue, I wrote this;
void f_output(char arg1[6], int limit);
void f_output(char *s, int limit) {
int x, i;
for (i=0; i < limit; i++)
printf("%s\n", s++);
}
You have a disagreement between the function declaration
and the function definition. In one place you used char arg1[6]
and in the other place you used char *s -- array vs pointer.
Those are the same thing. The 6 is ignored.
Brian
mdh wrote:
In trying to understand the issue, I wrote this;
#include <stdio.h>
void f_output(char arg1[6], int limit);
int main () {
f();
return 0;
}
void f(void) {
char matrix[2][6] ={ {'h','e','l','l','o','\0'},
{'w','o','r','l','d','\0'}};
f_output(matrix, 10);
}
void f_output(char *s, int limit) {
int x, i;
for (i=0; i < limit; i++)
printf("%s\n", s++);
}
Desired output: "Hello world"
Actual output:
hello
ello
llo
lo
o
world
orld
rld
ld
My naive way of looking at this was to assume that the declaration
"void f_output(char arg1[6], int limit)"
would "tell" the pointer that the type was an array of 6 chars, hence
s ++ would increment to matrix[2]. I understand this is a totally
useless and pointless function, except in trying to further my
understanding...so please go easy!!! :-)
I recommend that you stop naively assuming things and read your text
and/or the FAQs. You don't have an array definition there, as you can't
have array parameters. Arrays are converted to a pointer to the first
element of the array.
These are all the same:
void f(char *s);
void f(char s[]);
void f(char s[6]);
You should have received a diagnostic for passing s to that function,
as it's the wrong type. Here's what I got:
d:\arp\main.c(5) : warning C4013: 'f' undefined; assuming extern
returning int
d:\arp\main.c(10) : error C2371: 'f' : redefinition; different basic
types
d:\arp\main.c(13) : warning C4047: 'function' : 'char *' differs in
levels of indirection from 'char [2][6]'
d:\arp\main.c(13) : warning C4024: 'f_output' : different types for
formal and actual parameter 1
"mdh" <m...@comcast.netwrote:
In trying to understand the issue, I wrote this;
#include <stdio.h>
void f_output(char arg1[6], int limit);
>From a style point of view, the following is more consistent...
void f_output(char arg1[], int limit);
int main () {
f();
In C90, f is implicitly declared as int f(). In C99, this
violates a constraint (because f does not have a type since
it was never declared.)
return 0;
}
void f(void) {
This conflicts with the previous implicit declaration as it has
a void return type. The behaviour of the call to f in main() is
undefined.
char matrix[2][6] ={ {'h','e','l','l','o','\0'},
{'w','o','r','l','d','\0'}};
Simpler is...
char matrix[][6] = { "Hello", "World" };
f_output(matrix, 10);
This requires a diagnostic since matrix will decay to a pointer
to its first element. Thus it is equivalent to &matrix[0] which
has type char[6]. But that type is incompatible with the parameter
which has type char * (not char []).
}
void f_output(char *s, int limit) {
int x, i;
for (i=0; i < limit; i++)
printf("%s\n", s++);
}
Desired output: "Hello world"
Actual output:
<snip>
No C implementation is required to accept this program.
Turn up the warning levels and work on correct programs. It is
dangerous to try and learn from ill formed programs.
My naive way of looking at this was to assume that the declaration
"void f_output(char arg1[6], int limit)"
would "tell" the pointer that the type was an array of 6 chars,
Nope. This is a quirk of C. It is not possible to pass or return
arrays by value. A parameter declaration of type array will be
implicitly treated as a pointer to its element type.
As Eric mentions, the clc FAQ is worth reading.
hence s++ would increment to matrix[2].
If you believed that, why did you intent to increment s 10 times
when you know there are only 2 elements in matrix?
I understand this is a totally useless and pointless function,
except in trying to further my understanding...so please go
easy!!! :-)
Don't try to learn C by naive experimentation. Replicate samples
that are known to work. Explore what you _can_ do, not what you
think _might_ work.
--
Peter
Thank you all for replying.
On 11 Apr 2007 14:26:51 -0700, "mdh" <md**@comcast.netwrote:
>In trying to understand the issue, I wrote this;
#include <stdio.h> void f_output(char arg1[6], int limit); int main () {
f();
return 0; }
void f(void) { char matrix[2][6] ={ {'h','e','l','l','o','\0'}, {'w','o','r','l','d','\0'}}; f_output(matrix, 10);
Did your compiler not produce a diagnostic here. The first argument
to f_output must be of type char* to satisfy both the prototype above
and the actual definition below. Your are passing an argument of type
char (*)[6]. They are incompatible types. My recommendation is to
define the arguments in both places as
(char arg1[2][6], int limit)
>}
void f_output(char *s, int limit) { int x, i; for (i=0; i < limit; i++)
printf("%s\n", s++);
}
Desired output: "Hello world" Actual output:
hello ello llo lo o
world orld rld ld
Since you invoke undefined behavior, any output or no output would
correct.
>
My naive way of looking at this was to assume that the declaration
"void f_output(char arg1[6], int limit)"
Naive and incorrect. When an array is passed to a function, the
actual argument is a pointer to the first element of the array. That
is why it is OK for your prototype to have char arg1[6] while your
function definition has char *s. And you should have no trouble
figuring out what s++ will do when s is a char*.
> would "tell" the pointer that the type was an array of 6 chars, hence s ++ would increment to matrix[2]. I understand this is a totally useless and pointless function, except in trying to further my understanding...so please go easy!!! :-)
Remove del for email
Eric Sosman wrote:
Ian Collins wrote On 04/11/07 17:37,:
>>mdh wrote:
>>>In trying to understand the issue, I wrote this;
#include <stdio.h> void f_output(char arg1[6], int limit); [...]
void f_output(char *s, int limit) {
Head the warning your compiler should have given you here - this doesn't match the prototype.
As far as I can see, the definition and declaration
match perfectly. Yet both you and Walter Roberson say
they disagree ... In light of 6.3.5.7p7
A declaration of a parameter as "array of /type/"
shall be adjusted to "qualified pointer to /type/"
[...]
.... could one or both of you explain the disagreement?
My interpretation was that this applied where the parameter is type[]
and not where it is type[6]. The former being a generic array, the
latter and array of 6 type. It looks like I was mistaken.
--
Ian Collins.
Thanks to all. I know everyone approaches C differently, but I
certainly often learn more from trying...as long as this does not
exasperate all of you...but then I am sure, some would let me
know!!! :-)
mdh said:
Thanks to all. I know everyone approaches C differently, but I
certainly often learn more from trying...as long as this does not
exasperate all of you...but then I am sure, some would let me
know!!! :-)
Um, any article posted here is bound to exasperate some people. Your
article will exasperate those who don't like non-technical back-chat,
and this reply will exasperate those who don't like protracted
non-technical back-chat.
So don't worry /too/ much about that - as long as you stick to the
subject of programming in the C language and behave reasonably
cluefully (as you appear to have done so far), very few people will
have any reasonable cause to be genuinely exasperated with you.
Not that it will stop some of us trying! :-)
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www.
[given]
>char matrix[2][6] = ... f_output(matrix, 10);
In article <11**********************@q75g2000hsh.googlegroups .com>
Peter Nilsson <ai***@acay.com.auwrote:
>... matrix will decay to a pointer to its first element. Thus it is equivalent to &matrix[0] which has type char[6].
Actually char (*)[6], which is a very odd-looking type (and the
parentheses are in fact required). To declare a local variable
with such a type, the name goes after the "*", before the close
parenthesis, e.g.:
char (*p)[6] = &matrix[0];
To turn a declaration into a type-name, one simply removes the
variable name from the declaration, hence the odd-looking type.
The declaration itself is also odd-looking, but at least here
there is an "obvious" reason for the parentheses: without them
the brackets would bind more tightly than the "*". That is:
char *q[6];
"means":
char *(q[6]);
which declares q as a variable of type "array 6 of pointer to
char". We want "pointer to array 6 of char", so we need to bind
the "*" first, even when the variable name is omitted.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Chris Torek <nos...@torek.netwrote:
char matrix[2][6] = ...
f_output(matrix, 10);
Peter Nilsson <a...@acay.com.auwrote:
... matrix will decay to a pointer to its first element. Thus
it is equivalent to &matrix[0] which has type char[6].
Actually char (*)[6],
Yes. Thanks for the correction.
--
Peter
On Apr 11, 11:26 pm, "mdh" <m...@comcast.netwrote:
In trying to understand the issue, I wrote this;
#include <stdio.h>
void f_output(char arg1[6], int limit);
int main () {
f();
return 0;
}
void f(void) {
char matrix[2][6] ={ {'h','e','l','l','o','\0'},
{'w','o','r','l','d','\0'}};
f_output(matrix, 10);
}
void f_output(char *s, int limit) {
int x, i;
for (i=0; i < limit; i++)
printf("%s\n", s++);
}
Desired output: "Hello world"
Actual output:
hello
ello
llo
lo
o
world
orld
rld
ld
My naive way of looking at this was to assume that the declaration
"void f_output(char arg1[6], int limit)"
would "tell" the pointer that the type was an array of 6 chars, hence s
++ would increment to matrix[2]. I understand this is a totally
useless and pointless function, except in trying to further my
understanding...so please go easy!!! :-)
Hi.
I will explain u.
First, ur code won't compile, u have to modify a little like this:
#include <stdio.h>
void f_output(char arg1[6], int limit);
void f(void) {
char matrix[2][6] ={ {'h','e','l','l','o','\0'},
{'w','o','r','l','d','\0'}};
f_output(matrix, 10);
}
int main () {
f();
getch();
return 0;
}
void f_output(char *s, int limit) {
int x, i; //int x is't used
for (i=0; i < limit; i++)
printf("%s\n", s++);
}
The only difference is i put the void f(void) function's declaration
before it's called.
In C u can overload a function. It means u declare a function with the
same name but with not the same paramteres.
Anyway, u call the void f_output(char *s, int limit) function, this
void f_output(char arg1[6], int limit) does do nothing.
Ur decrlared string Hello World looks like this in the memory: 'h' 'e'
'l' 'l' 'o' '\0' 'w' 'o' 'r' 'l' 'd' '\0' and these characters are one
after the other.
When u call the function void f_output(char *s, int limit) s will b
the memory address of h. U call the function printf with s. So Hello
will b printed. Then u raise the memory address. s contains the
addresss of e. So when u call printf with the second time it will
print ello and so on. there is an empty line between Hello and World
and at the end, this is because u call the printf when s contains the
address of a null character, so it dosn't print anything. ra******@gmail.com wrote:
I will explain u.
Please don't use shorthand code like "u". Your post, frankly, is hard
enough to read without that sort of thing.
First, ur code won't compile, u have to modify a little like this:
#include <stdio.h>
void f_output(char arg1[6], int limit);
void f(void) {
char matrix[2][6] ={ {'h','e','l','l','o','\0'},
{'w','o','r','l','d','\0'}};
f_output(matrix, 10);
}
int main () {
f();
getch();
There's no such C function as getch(), and you didn't define one.
return 0;
}
void f_output(char *s, int limit) {
int x, i; //int x is't used
for (i=0; i < limit; i++)
printf("%s\n", s++);
}
The only difference is i put the void f(void) function's declaration
before it's called.
That wasn't the main problem, so your fix didn't help much.
In C u can overload a function. It means u declare a function with the
same name but with not the same paramteres.
That's not true. You are perhaps thinking of C++.
Anyway, u call the void f_output(char *s, int limit) function, this
void f_output(char arg1[6], int limit) does do nothing.
This make no sense, and I suspect you think that the two declarations
are different. They aren't.
The program has pretty been analyzed by the people here, and correct
given.
Brian ra******@gmail.com wrote, On 13/04/07 19:57:
<snip>
In C u can overload a function. It means u declare a function with the
same name but with not the same paramteres.
No you can't. C++ may well have such a facility, but that is a different
language.
Also, please do not use contractions like "u" for "you" and "ur" for
"your", it makes it far harder to read. In fact, it made it so hard for
me I gave up, it was just luck I spotted the word "overload" and so
realised you were saying things that are wrong.
<snip>
--
Flash Gordon This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Bret |
last post by:
I'm curious why char** argv is acceptable in the main() declaration.
In the comp.lang.c FAQ (question 6.18) it says that pointers to
pointers and pointers to an array are not interchangable. ...
|
by: Sona |
last post by:
I understand the problem I'm having but am not sure how to fix it. My
code passes two char* to a function which reads in some strings from a
file and copies the contents into the two char*s. Now...
|
by: sieg1974 |
last post by:
Hi,
I have made this simple program to understand char ** pointers, but I
still having many questions.
int main()
{
char ** testPointerPointerChar = 0;
char * A = "string01";
|
by: gaga |
last post by:
I can't seem to get this to work:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *names;
char **np;
|
by: jab3 |
last post by:
(again :))
Hello everyone.
I'll ask this even at risk of being accused of not researching adequately.
My question (before longer reasoning) is: How does declaring (or defining,
whatever) a...
|
by: max |
last post by:
Dear all,
I did the following analysis to conclude that the following pointer
types are not compatible. Please let me know If my analysis and
interpretation of the C standard are correct:
...
|
by: Xavier Roche |
last post by:
Hi folks,
I have a probably rather silly question: is casting a char array in a
char* a potential source of aliasing bug ?
Example: a fonction returning a buffer taken in a circular buffer
...
|
by: arnuld |
last post by:
i see the use of pointers, from K&R2 but what is the use of:
1. "pointer to pointer":
char c;
char** ppc;
2. pointer to function:
|
by: arnuld |
last post by:
int main()
{
const char* arr = {"bjarne", "stroustrup", "c++"};
char* parr = &arr;
}
this gives an error:
$ g++ test.cpp
test.cpp: In function 'int main()':
test.cpp:4: error: cannot...
|
by: Paul Brettschneider |
last post by:
Hello all,
consider the following code:
typedef char T;
class test {
T *data;
public:
void f(T, T, T);
void f2(T, T, T);
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |