473,385 Members | 1,730 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,385 software developers and data experts.

is this legal declaration/not correct output

Hi!
I have written some peace of code, but I wonder if it's legal for ansi-c
I have no problem to compiling it, but since I'm inexperience and the
output is not correct I have doubts. Thank you for help!

#include <stdio.h>
#include <stdlib.h>

#define __buff(s, c) s_c

int main(){

int i;
for(i=0;i<10;i++){
int __buff(*send,i);
}

for(i=0;i<10;i++){
__buff(send,i) = i;
}
for(i=0;i<10;i++){
printf("%d \n",__buff(send,i));
}

return 0;
}

The output is always 3...
My intentions is to deal with several int pointer with loops, since I'm
lazy to write all int's...
I want to make X number of int pointer

int *pointer_1
.....
int *pointer_X

allocate memory for all
pointer_1=malloc(sizeof( *pointer_1))
.....
pointer_X=malloc(sizeof( *pointer_X))
fill them with values
and do something

if this way not proper is there a "hack" for lazy programmer like me?

Thank you again!

L R
May 16 '07 #1
13 2176
sorry... old code.. :) here is the "right version"

#include <stdio.h>
#include <stdlib.h>

#define __buff(s, c) s_c

int main(){

int i;
int __buff(*send, ZERO);
for(i=0;i<4;i++){
int __buff(*send,i);
}
for(i=0;i<4;i++){
__buff(send,i) = malloc(sizeof (__buff(*send,i)));
}

for(i=0;i<4;i++){
__buff(send,i) = i;
}
for(i=0;i<4;i++){
printf("%d \n",__buff(send,i));
}

return 0;
}

May 16 '07 #2
In article <46***********************@news.luth.se>,
Carramba <us**@example.netwrote:
>#include <stdio.h>
#include <stdlib.h>

#define __buff(s, c) s_c
That defines an object-like macro __buff() that takes two parameters,
but no matter what the parameters are, always emits the variable name
s_c . Literally, ess underscore cee, totally unrelated to the s
and c parameters given to the macro. The output behaviour you
observe can be deduced from this.

You cannot just join parameters together in the manner you were thinking;
it -is- possible to create new identifiers from macro parameters, but
it requires fancier preprocessor work, and the replacement is
always done at -compile- time, not at execution time.

I would suggest that what you want to do is use arrays, not
try to generate new variable names dynamically.
>int main(){

int i;
int __buff(*send, ZERO);
for(i=0;i<4;i++){
int __buff(*send,i);
}
for(i=0;i<4;i++){
__buff(send,i) = malloc(sizeof (__buff(*send,i)));
}

for(i=0;i<4;i++){
__buff(send,i) = i;
}
for(i=0;i<4;i++){
printf("%d \n",__buff(send,i));
}

return 0;
}
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
May 16 '07 #3
Walter Roberson skrev:
In article <46***********************@news.luth.se>,
Carramba <us**@example.netwrote:
>#include <stdio.h>
#include <stdlib.h>

#define __buff(s, c) s_c

That defines an object-like macro __buff() that takes two parameters,
but no matter what the parameters are, always emits the variable name
s_c . Literally, ess underscore cee, totally unrelated to the s
and c parameters given to the macro. The output behaviour you
observe can be deduced from this.

You cannot just join parameters together in the manner you were thinking;
I was afraid so
it -is- possible to create new identifiers from macro parameters, but
it requires fancier preprocessor work, and the replacement is
always done at -compile- time, not at execution time.
could you by kind and suggest how to do it?
I would really like to learn some fancy c stuff :) is it ok that they
are created at compile time.
I would suggest that what you want to do is use arrays, not
try to generate new variable names dynamically.
May 16 '07 #4
Carramba <us**@example.netwrites:
sorry... old code.. :) here is the "right version"

#include <stdio.h>
#include <stdlib.h>

#define __buff(s, c) s_c
Identifiers starting with underscores are reserved to the
implementation. It's more complex than that, but you should avoid
using such identifiers them in your own code.

This macro ignores its arguments and expands to the identifier "s_c".
I doubt that that's what you intended. You might want to look into
the "##" operator.

You've managed to write code that compiles, but that doesn't do
anything resembling what you're trying to do.
int main(){
Ok, but "int main(void)" is more explicit.
int i;
int __buff(*send, ZERO);
This simply expands to

int s_c;
for(i=0;i<4;i++){
int __buff(*send,i);
You declare another variable named "s_c" inside your loop, but you
don't use it.
}
for(i=0;i<4;i++){
__buff(send,i) = malloc(sizeof (__buff(*send,i)));
This expands to
s_c = malloc(sizeof s_c);

Since s_c is an int, and malloc() returns a void* (and the compiler
knows it, since you have the correct "#include <stdlib.h>" directive),
this is illegal (actually a constraint violation; the compiler is
required to issue a diagnostic, but it's allowed to accept it).

More briefly, your compiler almost certainly warned you about a type
mismatch, but you ignored the warning. If your compiler *didn't* warn
you, crank up its warning levels until it does.
}

for(i=0;i<4;i++){
__buff(send,i) = i;
s_c = i;
}
for(i=0;i<4;i++){
printf("%d \n",__buff(send,i));
printf("%d \n",s_c);
}

return 0;
}
I can really only guess what you're *trying* to do. Looking at a
snippet of your code:

#define __buff(s, c) s_c
/* ... */

for(i = 0; i < 4; i++) {
int __buff(*send,i);
}

I *think* you're trying to create the following declarations:

int *send_0;
int *send_1;
int *send_2;
int *send_3;

The preprocessor just doesn't work that way. If your __buff() macro
were defined correctly, using the ## operator, the line would expand
to

int *send_i;

Macro expansion does textual substitution. The *value* of i is not
available to the preprocessor; it doesn't exist until your program is
actually running.

And even if that worked, the scope of the variable declaration is just
the compound statement. A new variable is created on each iteration
of the loop, and destroyed on reaching the closing '}'.

Basically, you've badly mixed up compilation time and execution time.

You're trying to create a numbered sequence of variables.

That's called an array:

int *send[4];

--
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"
May 16 '07 #5
Keith Thompson skrev:
Carramba <us**@example.netwrites:
>sorry... old code.. :) here is the "right version"
I can really only guess what you're *trying* to do. Looking at a
snippet of your code:
I posted in my first help request.. but with wrong code. and then
reposting code I just forgot to repost question ... seem like a lot
mistakes today :(

My intentions is to deal with several int pointer with loops, since I'm
lazy to write all int's...
I want to make X number of int pointer

int *pointer_1
......
int *pointer_X

allocate memory for all
pointer_1=malloc(sizeof( *pointer_1))
......
pointer_X=malloc(sizeof( *pointer_X))
fill them with values
and do something

if this way not proper is there a "hack" for lazy programmer like me?

Thank you again!
>
#define __buff(s, c) s_c
/* ... */

for(i = 0; i < 4; i++) {
int __buff(*send,i);
}

I *think* you're trying to create the following declarations:

int *send_0;
int *send_1;
int *send_2;
int *send_3;
yes I was *hopping* to by able to achieve this
The preprocessor just doesn't work that way. If your __buff() macro
were defined correctly, using the ## operator, the line would expand
to

int *send_i;

Macro expansion does textual substitution. The *value* of i is not
available to the preprocessor; it doesn't exist until your program is
actually running.

And even if that worked, the scope of the variable declaration is just
the compound statement. A new variable is created on each iteration
of the loop, and destroyed on reaching the closing '}'.

Basically, you've badly mixed up compilation time and execution time.
I understand that now...
You're trying to create a numbered sequence of variables.

That's called an array:

int *send[4];
good idea! this is one possible solution!
I kind of enjoy using macros, and this is the result of *over using*
it... this is bit a shame that one can create preprocessor loop.. some
times would make life easier.

Thank you.

L R
May 16 '07 #6
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <46***********************@news.luth.se>,
Carramba <us**@example.netwrote:
>>#include <stdio.h>
#include <stdlib.h>

#define __buff(s, c) s_c

That defines an object-like macro __buff() that takes two parameters,
but no matter what the parameters are, always emits the variable name
s_c . Literally, ess underscore cee, totally unrelated to the s
and c parameters given to the macro. The output behaviour you
observe can be deduced from this.
[...]

Quibble: it's a function-like macro, not an object-like macro.
But yes, it ignores its arguments.

--
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"
May 16 '07 #7
Keith Thompson skrev:
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
>In article <46***********************@news.luth.se>,
Carramba <us**@example.netwrote:
>>#include <stdio.h>
#include <stdlib.h>

#define __buff(s, c) s_c
That defines an object-like macro __buff() that takes two parameters,
but no matter what the parameters are, always emits the variable name
s_c . Literally, ess underscore cee, totally unrelated to the s
and c parameters given to the macro. The output behaviour you
observe can be deduced from this.
[...]

Quibble: it's a function-like macro, not an object-like macro.
But yes, it ignores its arguments.
why is that
#define min(X, Y) ((X) < (Y) ? (X) : (Y))
calling min(1,2) will expend to ((1) < (2) ? (1) : (2))

but
#define __buff(s, c) s_c
calling __buff(1,2) will expand to s_c and not to 1_2 ?
is this because of '_' ?
would
#define __buff(s, c) sc
__buff(1,2) =make 12 ?
May 16 '07 #8
In article <46***********************@news.luth.se>,
Carramba <us**@example.netwrote:
>My intentions is to deal with several int pointer with loops, since I'm
lazy to write all int's...
I want to make X number of int pointer

int *pointer_1
.....
int *pointer_X

allocate memory for all
pointer_1=malloc(sizeof( *pointer_1))
.....
pointer_X=malloc(sizeof( *pointer_X))
fill them with values
and do something
>if this way not proper is there a "hack" for lazy programmer like me?
No. The closest you could come would be a macro, perhaps called
Declare_and_malloc, that you would use like

Declare_and_malloc(pointer,1);
Declare_and_malloc(pointer,2);

and so on for each value, with the macro expanding to

int *pointer_1 = malloc(sizeof(*pointer_1));
int *pointer_2 = malloc(sizeof(*pointer_2));

and so on.

But there is no way to do something like:

#for J(1,10) Declare_and_malloc(pointer,J);

and have it generate the Declare_and_malloc(pointer,1);
and Declare_and_malloc(pointer,2); and so on up to 10.

Use arrays instead:

int *pointers[10];
for (j = 1; j < 10; j++)
pointers[j] = malloc(sizeof **pointers);

--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
May 16 '07 #9
In article <46***********************@news.luth.se>,
Carramba <us**@example.netwrote:
>why is that
#define min(X, Y) ((X) < (Y) ? (X) : (Y))
calling min(1,2) will expend to ((1) < (2) ? (1) : (2))
>but
#define __buff(s, c) s_c
calling __buff(1,2) will expand to s_c and not to 1_2 ?
is this because of '_' ?
would
#define __buff(s, c) sc
__buff(1,2) =make 12 ?
No. Macros work on the basis of matching tokens, and _ is a valid
part of a token name. In

#define __buff(s,c) s_c

the preprocessor sees the single token s_c not the three tokens
s _ c .

It is possible to put tokens together, but the steps necessary are
obscure:
#define Join3b(a,b,c) a ## b ## c
#define Join3(a,b,c) Join3b(a,b,c)

then

Join3(s,_,c) would expand to s_c

It is -necessary- to use a two-step process to get this to work.
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
May 16 '07 #10
Carramba <us**@example.netwrites:
[...]
why is that
#define min(X, Y) ((X) < (Y) ? (X) : (Y))
calling min(1,2) will expend to ((1) < (2) ? (1) : (2))

but
#define __buff(s, c) s_c
calling __buff(1,2) will expand to s_c and not to 1_2 ?
is this because of '_' ?
would
#define __buff(s, c) sc
__buff(1,2) =make 12 ?
Let's get rid of the leading underscores:

#define buff(s, c) s_c

buff(1, 2) /* expands to s_c; the arguments are ignored */

"s_c" is simply a single identifier. In an identifier, the '_'
character acts very much like a letter (except for the fact that
leading underscores can cause problems). s_c is a token by itself;
it's *not* composed of separate tokens s and c separated by an
underscore.

Similarly:

#define buff(s, c) sxc
buff(1, 2) /* expands to sxc; the arguments are ignored */

It is possible to paste tokens together in macro expansions, using the
"##" operator. Any decent C textbook should explain this (I recommend
Kernighan & Ritchie's _The C Programming Language_, known to its
friends as K&R2). I'm not going to go into detail here because (a)
you're likely to learn more if you do the research yourself (and I
might get something wrong), and (b) it's not a good solution to your
problem.

--
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"
May 16 '07 #11
Carramba wrote:
>
I have written some peace of code, but I wonder if it's legal for
ansi-c I have no problem to compiling it, but since I'm inexperience
and the output is not correct I have doubts. Thank you for help!

#include <stdio.h>
#include <stdlib.h>

#define __buff(s, c) s_c
identifiers starting with an underscore are generally reserved for
the implementation. Don't use them.
>
int main(){
Better to specify the parameters. In this case use "int
main(void)".
>
int i;
for(i=0;i<10;i++){
Separate things with spaces.
int __buff(*send,i);
In general (especially C90) you can't specify storage after code.
Macros just do text replacement. This is meaningless, with s_c
undefined.
}

for(i=0;i<10;i++){
__buff(send,i) = i;
}
for(i=0;i<10;i++){
printf("%d \n",__buff(send,i));
}

return 0;
}

The output is always 3...
You have some work to do.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 17 '07 #12
Carramba skrev:
>
L R
thank you all!
May 17 '07 #13
On Wed, 16 May 2007 22:46:47 -0400, CBFalconer <cb********@yahoo.com>
wrote:
<snip: several good points>
int i;
for(i=0;i<10;i++){

Separate things with spaces.
int __buff(*send,i);
(Which actually expands to int s_c;)
In general (especially C90) you can't specify storage after code.
In C90 (really C<99) you can't have declarations after statements
_within a block_. Since the body of this for statement is a compound
statement == block, a declaration at its beginning is perfectly legal.
But it is effective only within that block, and does not declare a
variable usable _after_ this for loop ends, much less a series of such
variables, as the OP wanted.
Macros just do text replacement. This is meaningless, with s_c
undefined.
Just text replacement yes, but the result int s_c; is a perfectly
valid though useless declaration that locally defines s_c.
}
<snip rest>
- formerly david.thompson1 || achar(64) || worldnet.att.net
Jul 1 '07 #14

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

Similar topics

3
by: Chris Johnson | last post by:
Greetings all: I come across an interesting question (to me anyway) and I do not know the answer. Code/Questions follow: #include <iostream> #if 0 // uncommenting *should* make call...
4
by: Peter C. Chapin | last post by:
I have a need to include Greek letters in some of my XML documents (the documents contain astronomical information and many stars are named using Greek letters). Following some earlier postings on...
6
by: Teddy | last post by:
Hello all simple code below: void foo() { } void foo(int i = 0) { } int main() { //foo(); // call of overloaded 'foo()' is ambiguous
11
by: puzzlecracker | last post by:
is it possible to call nonconst member function on an unnamed temprary? ex: class String{ public: void doSomething(); }; String createString();
3
by: b83503104 | last post by:
Hi, I wonder if this is a legal array declaration? const int size = 5; double array; Or should I use "new"? Thanks.
8
by: Mark P | last post by:
I was helping a coworker port some code to a new compiler (HP). He had something along the lines of this: class C { // defined in the global namespace static int foo(); }; namespace { int...
1
by: HappyHippy | last post by:
Hi, I have a question about forward declaration of classes. First of all, a simple working example, which compiles and works with no problems. ///file class_a.h:/ #ifndef __CLASS_A_H__...
17
by: lovecreatesbeauty | last post by:
1. In the following code, is the code (line 11) legal? Is there a notice in the document to tell callers that the parameter s1 should receive an array variable, i.e. type char, but not a variable...
36
by: Max | last post by:
This was asked in a C test. Is the following code legal C? Is it legal in C90? C99? #define main() int main #define mainbody () { return 0; } mainbody
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...
0
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.