473,394 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Pointer Ambiguity

Please read the comments.
Program to multiply the elements of an array with its size.
#define s 5
#include<stdio.h>

void mul(int *p)
{
for(int i=0;i<s;i++)
{
printf("%d ",*p*s);
p++;
}
}

main()
{
int a[s],i;
printf("Enter %d No. ",s);
for(i=0;i<s;i++)
scanf("%d",&a[i]);
mul(a);/* it should be mul(&a) why not?*/
return 0;
}

Jun 17 '07 #1
16 1370
Umesh wrote:
Please read the comments.
Come back when you learn how to write main correctly.
>
main()
--
Ian Collins.
Jun 17 '07 #2
Ian Collins said:
Umesh wrote:
>Please read the comments.

Come back when you learn how to write main correctly.
>>
main()
What's incorrect about that?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 17 '07 #3
On Jun 17, 12:40 pm, Richard Heathfield <r...@see.sig.invalidwrote:
Ian Collins said:
Umesh wrote:
Please read the comments.
Come back when you learn how to write main correctly.
main()

What's incorrect about that?
Ideally that should be
int main(void)

thanks
>
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.

Jun 17 '07 #4
Richard Heathfield <rj*@see.sig.invalidwrites:
Ian Collins said:
>Umesh wrote:
>>Please read the comments.

Come back when you learn how to write main correctly.
>>>
main()

What's incorrect about that?
In C90, it's correct (but poor style). In C99, it's illegal. Since
the code uses a declaration in a for loop:

for(int i=0;i<s;i++)

we can infer that it's intended to be C99, and that main is declared
incorrectly. Or that it's intended to be C90, and the for loop is
written incorrectly. Or, more likely, that the compiler is invoking
his compiler in a mode that conforms to neither standard, and doesn't
know or care whether his code is correct.

--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 17 '07 #5

"blufox" <25**********@gmail.comwrote in message
news:11**********************@j4g2000prf.googlegro ups.com...
On Jun 17, 12:40 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>Ian Collins said:
Umesh wrote:
Please read the comments.
Come back when you learn how to write main correctly.
>main()

What's incorrect about that?

Ideally that should be
int main(void)

thanks
Ideally.
But implicit int is conforming, and arguably a good idea, since it demotes
size_t.
An empty argument list means that the function takes no or more parameters.
So the fact that main(void) is allowed is a quirk.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jun 17 '07 #6
Keith Thompson <ks***@mib.orgwrites:
[...]
we can infer that it's intended to be C99, and that main is declared
incorrectly. Or that it's intended to be C90, and the for loop is
written incorrectly. Or, more likely, that the compiler is invoking
his compiler in a mode that conforms to neither standard, and doesn't
know or care whether his code is correct.
Whoop, I meant that the *original poster* is invoking his compiler in
a mode that etc.

--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 17 '07 #7
Richard Heathfield wrote:
Ian Collins said:
>Umesh wrote:
>>Please read the comments.
Come back when you learn how to write main correctly.
>>main()

What's incorrect about that?
The fact (as Keith beat me to pointing out) that it was associated with
this:

for(int i=0;i<s;i++)

--
Ian Collins.
Jun 17 '07 #8
Malcolm McLean wrote, On 17/06/07 09:32:
>
"blufox" <25**********@gmail.comwrote in message
news:11**********************@j4g2000prf.googlegro ups.com...
>On Jun 17, 12:40 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>>Ian Collins said:

Umesh wrote:
Please read the comments.

Come back when you learn how to write main correctly.

main()

What's incorrect about that?

Ideally that should be
int main(void)

thanks
Ideally.
But implicit int is conforming, and arguably a good idea, since it
demotes size_t.
Implicit int has nothing to do with size_t so I really can't see what
you are trying to say.
An empty argument list means that the function takes no or more
parameters. So the fact that main(void) is allowed is a quirk.
It is still a good idea because it is the only way to ensure the
compiler will complain if you then call main recursively passing it
parameters. This is more important for other functions (where it does
not only apply to recursive calls), but it is a good habit.
--
Flash Gordon
Jun 17 '07 #9
Ian Collins said:
Richard Heathfield wrote:
>Ian Collins said:
>>Umesh wrote:
Please read the comments.
Come back when you learn how to write main correctly.
main()

What's incorrect about that?
The fact (as Keith beat me to pointing out) that it was associated
with this:

for(int i=0;i<s;i++)
Since he uses implicit int, I infer that he's writing in C90, in which
case it's the for loop that is incorrect.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 17 '07 #10
Umesh wrote:
Please read the comments.
Program to multiply the elements of an array with its size.
#define s 5
#include<stdio.h>
About the order the #define and the #include directives:
what happens if printf() is declared as:

int printf (const char * s, ...);

Won't the declaration be expanded into:

int printf (const char * 5, ...);

--
regis
Jun 17 '07 #11
regis wrote:
Umesh wrote:
>Please read the comments.
Program to multiply the elements of an array with its size.
>#define s 5
#include<stdio.h>

About the order the #define and the #include directives:
what happens if printf() is declared as:

int printf (const char * s, ...);

Won't the declaration be expanded into:

int printf (const char * 5, ...);
Yes, and because of that, <stdio.his not allowed to declare printf that
way.
Jun 17 '07 #12
Harald van Dijk wrote:
regis wrote:
>Umesh wrote:
>>Please read the comments.
Program to multiply the elements of an array with its size.
#define s 5
#include<stdio.h>
About the order the #define and the #include directives:
what happens if printf() is declared as:

int printf (const char * s, ...);

Won't the declaration be expanded into:

int printf (const char * 5, ...);

Yes, and because of that, <stdio.his not allowed to declare printf that
way.
so, is the use of double underscore prefixes for identifiers
or the use of no identifier at all
mandatory for standard headers ?
Jun 17 '07 #13
regis wrote:
Harald van Dijk wrote:
>regis wrote:
>>Umesh wrote:
Please read the comments.
Program to multiply the elements of an array with its size.
#define s 5
#include<stdio.h>
About the order the #define and the #include directives:
what happens if printf() is declared as:

int printf (const char * s, ...);

Won't the declaration be expanded into:

int printf (const char * 5, ...);

Yes, and because of that, <stdio.his not allowed to declare printf that
way.

so, is the use of double underscore prefixes for identifiers
or the use of no identifier at all
mandatory for standard headers ?
There are other reserved identifiers that could be used, but yes, standard
headers can't use identifiers for parameter names that user code is allowed
to use.
Jun 17 '07 #14
[given something like:
>>>#define s 5
#include<stdio.h>
which causes the identifier s to expand to the number 5 if an
unadorned "s" appears inside the contents of <stdio.h>, assuming
that <stdio.his an ordinary file that is expanded in the ordinary
way]
>>regis wrote:
>>>what happens if printf() is declared as:
int printf (const char * s, ...);
Won't the declaration be expanded into:
int printf (const char * 5, ...);
>Harald van Dijk wrote:
>>Yes, and because of that, <stdio.his not allowed to declare printf that
way.
In article <46**********************@news.free.fr>
regis <re**************@free.frwrote:
>so, is the use of double underscore prefixes for identifiers
or the use of no identifier at all
mandatory for standard headers ?
The C standards do not say *how* the implementation has to avoid
the problem, just that it must avoid the problem.

Two obvious and simple methods are, indeed, to use double-underscore
prefixes or omit identifier names, e.g.:

char *strcpy(char *restrict __dst, const char *restrict);

However, an implementor can use *any* reserved-to-the-implementor
name, e.g.:

char *strcpy(char *restrict memset, const char *restrict _E);

(which is admittedly kind of bizarre), or even compiler-specific
extensions such as:

#pragma implementor_namespace_hack
char *strcpy(char *restrict dst, const char *restrict src);
#pragma end_implementor_namespace_hack

or similar. If <stdio.hor -- as in this case -- <string.his
not, in fact, an ordinary file at all -- e.g., if it simply turns
on a flag in the compiler that says that the header has been included
-- there may even be no preprocessing phase involved, hence no
"name collision hazard" in the first place.
--
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.
Jun 17 '07 #15
Chris Torek wrote:
[given something like:
>>>>#define s 5
#include<stdio.h>

which causes the identifier s to expand to the number 5 if an
unadorned "s" appears inside the contents of <stdio.h>, assuming
that <stdio.his an ordinary file that is expanded in the ordinary
way]
[...]
If <stdio.hor -- as in this case -- <string.his
not, in fact, an ordinary file at all -- e.g., if it simply turns
on a flag in the compiler that says that the header has been included
-- there may even be no preprocessing phase involved, hence no
"name collision hazard" in the first place.
Ok. So, along these lines, I guess

#define printf foo
#include <stdlib.h>

also gives unpredictable results, that is, one cannot predict if

int foo (const char *, ...);

or

int printf (const char *, ...);

will be declared.
Jun 17 '07 #16

"regis" <re**************@free.frha scritto nel messaggio
news:46**********************@news.free.fr...
Chris Torek wrote:
>[given something like:
>>>>>#define s 5
>#include<stdio.h>

which causes the identifier s to expand to the number 5 if an
unadorned "s" appears inside the contents of <stdio.h>, assuming
that <stdio.his an ordinary file that is expanded in the ordinary
way]

[...]
>If <stdio.hor -- as in this case -- <string.his
not, in fact, an ordinary file at all -- e.g., if it simply turns
on a flag in the compiler that says that the header has been included
-- there may even be no preprocessing phase involved, hence no
"name collision hazard" in the first place.

Ok. So, along these lines, I guess

#define printf foo
#include <stdlib.h>

also gives unpredictable results, that is, one cannot predict if

int foo (const char *, ...);

or

int printf (const char *, ...);

will be declared.
From the C99 rationale, 6.10.3:
As with any other powerful language feature, keyword redefinition is subject
to abuse. Users

cannot expect any meaningful behavior to come about from source files
starting with

#define int double

#include <stdio.h>

or similar subversions of common sense.
Jun 17 '07 #17

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

Similar topics

69
by: Ken | last post by:
Hi all. When referring to a null pointer constant in C++, is there any reason to prefer using 0 over a macro called NULL that is defined to be 0? Thanks! Ken
51
by: BigMan | last post by:
Does the C++ standard define what should happen in case of NULL pointer dereferencing. If not, does it say that it is illegal? Where, if so, does it say it?
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
42
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
17
by: Divick | last post by:
Hi, I am designing an API and the problem that I have is more of a design issue. In my API say I have a class A and B, as shown below class A{ public: void doSomethingWithB( B * b) { //do...
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
9
by: Cyron | last post by:
Hello friends, Recently I have begun exploring the features that the STL map collection provides. While learning how it worked, I encountered a result that confused me. Up until now, I had...
17
by: let_the_best_man_win | last post by:
How do I print a pointer address into a string buffer and then read it back on a 64 bit machine ? Compulsion is to use string (char * buffer) only. printing with %d does not capture the full...
26
by: Ioannis Vranos | last post by:
Are the following guaranteed to work always as *C90* code? 1. #include <stdio.h> void some_func(int *p, const size_t SIZE) { size_t i;
49
by: Davy | last post by:
Hi all, I am writing a function, which return the pointer of the int. But it seems to be wrong. Any suggestion? int * get_p_t(int t) { return &t; } int main()
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.