L in return

K

Keith Thompson

frank said:
Why don't we see an H in stdout here?

dan@dan-desktop:~/source$ gcc -std=c99 -Wall -Wextra hi4.c -o out
dan@dan-desktop:~/source$ ./out
here in func

Hello, world
Hello, world
Hello, world
dan@dan-desktop:~/source$ cat hi4.c
[...]

We do, at the beginning of each of the three lines of output.

The array s was initialized to the strong "hello, world".
The assignment
*param = 'H';
changed the first character from 'h' to 'H'; the rest of the string
was untouched.
 
M

Morris Keesan

....
According to p. 387, H&S V, these are the two #includes you snipped.

If Harbison & Steele really say that you need two #includes to use wprintf,
then they're wrong. According to the standard, all you need for wprintf
is <wchar.h>. You don't need <stdio.h> unless you're using one of the
functions that takes a (FILE *) argument, such as fwprintf.
 
F

frank

Just as with printf, the first argument is the format string, which
specifies the number and type(s) of any following arguments. If the
format string contains no '%' characters (or if any '%' characters are
doubled), then the format string is (or should be) the only argument.

wprintf(L"One argument\n");
wprintf(L"Two arguments: %s\n", s1);
wprintf(L"Three arguments: %s %s\n", s1, s2); wprintf(L"Four
arguments: %s%s %s %s\n", s1, s2, s3);

Am I correct to think that wchar_t *a is compatible with const wchar_t *
restrict format in this context:

#include <stdio.h>
#include <wchar.h>

int my_function(wchar_t *a){
return wprintf(a);
}
int main(){
return my_function(L"hi there\n");
}

// gcc -std=c99 -Wall -Wextra hi1.c -o out

It's const by virtue of looking at the source and seeing nothing that
makes it unconst. The restrict is something that was added in recent
treatments. I have no idea what it's supposed to do.

If it is compatible, then I wonder what else you could throw at wprintf
in its first argument without doing something stupid or illegal.
 
B

Ben Bacarisse

frank said:
Am I correct to think that wchar_t *a is compatible with const wchar_t *
restrict format in this context:

Whether two types are compatible or not depends on the types
themselves are does not vary from one context to another. wchar_t *
is not compatible with either const wchar_t * or with const wchar_t *
restrict.

What you are discussing is something else that, sadly, does not have a
name. It is the property that type T1 can be passed as argument to
a function that has a parameter of T2 in that position. This is
closely related to question whether an expression of type T1 can be
assigned to a object of type T2 because parameter passing is defined
in terms of assignment.

Unfortunately neither of these two properties is symmetric, so we have
to careful when inverting a term. Maybe just using "may be assigned
to" and "passed to" will do as a shorthand. In that case, wchar_t *
may be passed to both const wchar_t * and to const wchar_t * restrict.

To take another example, wchar_t * may be passed to const wchar_t *
const but it may no be assigned to that type.

None of these different types are compatible with each other.

<snip>
 

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

Similar Threads

constructing robust constructors 15
L-value problem 5
wcslen function 2
calling a singly-linked list 16
when to ignore warnings 1
prototypes 4
herding the bits of a float into an integer array 35
faq 19.1 57

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top