473,775 Members | 2,189 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

char pointers?

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

Apr 11 '07 #1
17 2292
In article <11************ **********@b75g 2000hsg.googleg roups.com>,
mdh <md**@comcast.n etwrote:
>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
Apr 11 '07 #2
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.
Apr 11 '07 #3
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
Apr 11 '07 #4

"mdh" <md**@comcast.n etwrote in message
news:11******** **************@ b75g2000hsg.goo glegroups.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_l ist++);
}
}

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
Apr 11 '07 #5
Walter Roberson wrote:
In article <11************ **********@b75g 2000hsg.googleg roups.com>,
mdh <md**@comcast.n etwrote:
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
Apr 11 '07 #6
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(1 0) : error C2371: 'f' : redefinition; different basic
types
d:\arp\main.c(1 3) : warning C4047: 'function' : 'char *' differs in
levels of indirection from 'char [2][6]'
d:\arp\main.c(1 3) : warning C4024: 'f_output' : different types for
formal and actual parameter 1
Apr 11 '07 #7
"mdh" <m...@comcast.n etwrote:
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

Apr 12 '07 #8
mdh
Thank you all for replying.

Apr 12 '07 #9
On 11 Apr 2007 14:26:51 -0700, "mdh" <md**@comcast.n etwrote:
>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(matri x, 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
Apr 12 '07 #10

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

Similar topics

21
18888
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. However the declaration: int main(int argc, char** argv) is common.
5
2539
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 when my function returns, the values stored in the char* are some garbage values (perhaps because I didn't allocate any memory for them).. but even if I allocate memory in the function, on the return of this function I see garbage.. here is my...
3
2212
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";
19
14520
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;
5
3980
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 variable **var make it an array of pointers? I realize that 'char **var' is a pointer to a pointer of type char (I hope). And I realize that with var, var is actually a memory address (or at
5
5347
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: const char * : "pointer to const-qualified char". char *: "pointer to char". Are these pointed-to types compatibles?
4
2160
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 typedef struct foo_t foo_t; struct foo_t { int index;
13
3137
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:
21
2758
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 convert 'const char* (*)' to 'char*' in
4
3225
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);
0
9625
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10280
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10056
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7466
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5365
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5489
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4024
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3618
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2857
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.