#include <stdio.h>
main()
{
int *f(void);
f()[2] = 'x'; /* ------(1) */
printf("%c %c %c %c\n", f()[0],
f()[1], f()[2], f()[3]);
}
int *f(void)
{
static int a[] = {'a', 'b', 'c', 'd'};
return &a[0];
}
Output
a b x d
What is happening in 1st line. I am not able to understand. 10 1169
Kavya said:
>
#include <stdio.h>
main()
{
int *f(void);
f()[2] = 'x'; /* ------(1) */
printf("%c %c %c %c\n", f()[0],
f()[1], f()[2], f()[3]);
}
int *f(void)
{
static int a[] = {'a', 'b', 'c', 'd'};
return &a[0];
}
Output
a b x d
What is happening in 1st line. I am not able to understand.
It's a perfectly legal shorthand for:
int *p = f();
p[2] = 'x';
without going to all the trouble and inconvenience of defining a temporary
object p to store the pointer value returned by f.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Kavya wrote:
#include <stdio.h>
main()
{
int *f(void);
f()[2] = 'x'; /* ------(1) */
printf("%c %c %c %c\n", f()[0],
f()[1], f()[2], f()[3]);
}
int *f(void)
{
static int a[] = {'a', 'b', 'c', 'd'};
return &a[0];
}
Output
a b x d
What is happening in 1st line. I am not able to understand.
In C, a function that returns a value can be a part of a larger
expression. Here a call to f() returns a value of type pointer to int.
The array indexing operator is used on this value to increment it by 2
* sizeof(*f()), in order to modify the third element of f()'s static
array and display it with printf() by the same manner described above.
Richard Heathfield wrote:
Kavya said:
#include <stdio.h>
main()
{
int *f(void);
f()[2] = 'x'; /* ------(1) */
printf("%c %c %c %c\n", f()[0],
f()[1], f()[2], f()[3]);
}
int *f(void)
{
static int a[] = {'a', 'b', 'c', 'd'};
return &a[0];
}
Output
a b x d
What is happening in 1st line. I am not able to understand.
It's a perfectly legal shorthand for:
int *p = f();
p[2] = 'x';
without going to all the trouble and inconvenience of defining a temporary
object p to store the pointer value returned by f.
Is it supposed to work fine on Bound Checked Implementation?
Kavya wrote:
Richard Heathfield wrote:
Kavya said:
>
#include <stdio.h>
>
main()
{
int *f(void);
>
f()[2] = 'x'; /* ------(1) */
printf("%c %c %c %c\n", f()[0],
f()[1], f()[2], f()[3]);
}
>
int *f(void)
{
static int a[] = {'a', 'b', 'c', 'd'};
>
return &a[0];
}
>
Output
a b x d
>
>
What is happening in 1st line. I am not able to understand.
It's a perfectly legal shorthand for:
int *p = f();
p[2] = 'x';
without going to all the trouble and inconvenience of defining a temporary
object p to store the pointer value returned by f.
Is it supposed to work fine on Bound Checked Implementation?
I can't see any reason why it should not.
"Richard Heathfield" <in*****@invalid.invalidwrote in message
news:7r********************@bt.com...
Kavya said:
>> #include <stdio.h>
main() { int *f(void);
f()[2] = 'x'; /* ------(1) */ printf("%c %c %c %c\n", f()[0], f()[1], f()[2], f()[3]); }
int *f(void) { static int a[] = {'a', 'b', 'c', 'd'};
return &a[0]; }
Output a b x d
What is happening in 1st line. I am not able to understand.
It's a perfectly legal shorthand for:
int *p = f();
p[2] = 'x';
without going to all the trouble and inconvenience of defining a temporary
object p to store the pointer value returned by f.
At the cost of considerable trouble and inconvenience for Kavya, who has the
unhappy task of reading this code.
-- www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.
Kavya:
#include <stdio.h>
main()
Implicit int is a bad thing.
int main(void)
{
{
int *f(void);
f()[2] = 'x'; /* ------(1) */
Array subscripting works as follows in C.
arr[i]
is interpreted as:
*(arr + i)
"arr" implicitly converts to a pointer to the first element of the array,
then "i" is added to this address, then the whole thing is dereferenced.
So, your line of code is equivalent to:
*(f() + 2) = 'x';
The pointer which is returned from the invocation of "f" has 2 added to it,
then the whole thing is dereferenced yielding an L-value, and then an
assignment is performed upon the L-value.
--
Frederick Gotham
Frederick Gotham <fg*******@SPAM.comwrites:
Kavya:
>#include <stdio.h>
main()
Implicit int is a bad thing.
int main(void)
{
>{ int *f(void);
f()[2] = 'x'; /* ------(1) */
Array subscripting works as follows in C.
arr[i]
is interpreted as:
*(arr + i)
"arr" implicitly converts to a pointer to the first element of the array,
then "i" is added to this address, then the whole thing is dereferenced.
That implicit conversion happens only if arr is an array expression.
So, your line of code is equivalent to:
*(f() + 2) = 'x';
The pointer which is returned from the invocation of "f" has 2 added to it,
then the whole thing is dereferenced yielding an L-value, and then an
assignment is performed upon the L-value.
In this case, the value returned by f() is already a pointer, which is
what the indexing operator expects, so no implicit conversion is
necessary. (For the indexing operation to make sense, the pointer has
to be pointing to the first element of an array in memory, but it
still has to be a pointer not an array.)
We think of "[]" as an *array* indexing operator because the prefix is
most commonly an array name, but that's really just a special case; in
general, the prefix is a pointer. If the prefix is an array name,
it's converted to a pointer before the "[]" operator is applied.
(If you're not quite confused enough, read question 6.11 in the
comp.lang.c FAQ, <http://www.c-faq.com/>. Then read the rest of
section 6 if you want to be *less* cofused.)
(The above is a general "you", not directed at Frederick.)
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Context:
f()[2] = 'x';
Malcolm said:
"Richard Heathfield" wrote...
>> It's a perfectly legal shorthand for:
int *p = f(); p[2] = 'x';
without going to all the trouble and inconvenience of defining a temporary object p to store the pointer value returned by f.
At the cost of considerable trouble and inconvenience for Kavya, who has
the unhappy task of reading this code.
Yes. I'm not overly keen on it either, but occasionally I've been guilty of
the equivalent, *strchr(foo, bar) = baz, when I knew for sure that bar
appears in foo. Tsk tsk, as they say...
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Keith Thompson:
(The above is a general "you", not directed at Frederick.)
; )
It's funny... I think one can gauge one's progress in becoming proficient at
a programming language by counting how many times per day they enounter
something on comp.lang.X that really opens their eyes.
On comp.lang.c++, it's very rare for me.
On comp.lang.c, it's becoming less and less frequent.
--
Frederick Gotham This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Olaf |
last post by:
I have a frameset page witch contains the myFuc() function. The
function is accessed from a page in one of the frames in the frameset.
An example is shown below.
<input...
|
by: Bill C. |
last post by:
Hi,
I've got a simple console app that just reads an XML file into a DataSet
then prints out a description of each table in the DataSet, including
column names and row values for each column. ...
|
by: Neil Ginsberg |
last post by:
I have a strange situation with my Access 2000 database. I have code in the
database which has worked fine for years, and now all of a sudden doesn't
work fine on one or two of my client's...
|
by: Harvey Twyman |
last post by:
I have code written under the CCS 'C' Compiler to run on a PIC microcontroller.
Code Extract:
-------------------------------
char a,b,c;
-------------------------------
c = ( a == b );...
|
by: ralphsieminsky |
last post by:
I am seeing a strange behavior with a managed/unmanaged C++ program.
The program is a large application made of DLLs, COM components,
and an exe. When I recompile the exe with /clr a problem...
|
by: James Wong |
last post by:
Hi! everybody,
I'm facing a quite strange download problem. I use the following code to
download an XML file to client side:
With Response
' clear buffer
Call .Clear()
' specify the...
|
by: Jim Bancroft |
last post by:
Hi everyone,
I have a DropDownList I populate as outlined below. This is from my
code-behind file:
private void Page_Load(object sender, System.EventArgs e)
{
BindMyData();
DataBind();
}
|
by: WSobczuk |
last post by:
I have encountered a very strange error and I'm hoping that some Python
hackers here could give me insight on this.
searchview.py file contains two functions:
def mysearch(indexname, request, c,...
|
by: peter |
last post by:
Hi,
I have very strange situation but first description ;)
I have:
1) project in VB.NET, in this f.e. 1 function:
Public Function Login(ByVal UserName As String, ByVal UserPassword As...
|
by: John Kraft |
last post by:
Hello all,
I'm experiencing some, imo, strange behavior with the StreamReader
object I am using in the code below. Summary is that I am downloading
a file from a website and saving it to disk...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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...
|
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...
|
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,...
| |