char pointers?

M

mdh

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!!! :)
 
W

Walter Roberson

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.
 
I

Ian Collins

mdh said:
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.
 
E

Eric Sosman

Ian Collins wrote On 04/11/07 17:37,:
mdh said:
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.
 
J

Jonas

mdh said:
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);

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.
 
D

Default User

Walter said:
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
 
D

Default User

mdh said:
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
 
P

Peter Nilsson

mdh said:
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.
 
B

Barry Schwarz

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
 
I

Ian Collins

Eric said:
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.
 
M

mdh

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!!! :)
 
R

Richard Heathfield

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! :)
 
C

Chris Torek

[given]
char matrix[2][6] = ...
f_output(matrix, 10);

... 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.
 
R

rasztasd

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.
 
F

Flash Gordon

(e-mail address removed) wrote, On 13/04/07 19:57:

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>
 
D

Default User

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top