473,789 Members | 2,263 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
17 2294
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.
Apr 12 '07 #11
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!!! :-)

Apr 12 '07 #12
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.
Apr 12 '07 #13
[given]
>char matrix[2][6] = ...
f_output(matri x, 10);
In article <11************ **********@q75g 2000hsh.googleg roups.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.
Apr 13 '07 #14
Chris Torek <nos...@torek.n etwrote:
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

Apr 13 '07 #15
On Apr 11, 11:26 pm, "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);
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.

Apr 13 '07 #16
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
Apr 13 '07 #17
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
Apr 13 '07 #18

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

Similar topics

21
18894
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
14523
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
3981
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
2161
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
3138
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
3226
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
9657
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
9502
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10393
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...
0
10187
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9007
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6753
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4084
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
3687
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2902
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.