473,325 Members | 2,785 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,325 software developers and data experts.

basic #define usage

Hi,

Ashamed to ask basic question but I cannot get this code to run,

#define Y one

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

int main (void) {
//Y=malloc(sizeof(char *));
printf ("%s",Y);

return 0;

}

I can compile but not run, gives me "the memory cannot be read".
All I want is to printf Y.

Please help, I know this is easy for most of you.

Thanks.
Kel

Nov 13 '05 #1
3 4674
Kelly <ke***@invalid.com> writes:
#define Y one

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

int main (void) {
//Y=malloc(sizeof(char *));
printf ("%s",Y);

return 0;

}


Y expands to one.
one is undefined.
This won't compile.
--
"You call this a *C* question? What the hell are you smoking?" --Kaz
Nov 13 '05 #2
Kelly wrote:
Hi,

Ashamed to ask basic question but I cannot get this code to run,

#define Y one

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

int main (void) {
//Y=malloc(sizeof(char *));
printf ("%s",Y);

return 0;

}

I can compile but not run, gives me "the memory cannot be read".
All I want is to printf Y.

Please help, I know this is easy for most of you.


It's easy for anyone who checks the FAQ before posting.
Below is a version of your program which shows various behaviors that you
might one to check. The question and answer in the FAQ can be found at
<http://www.eskimo.com/~scs/C-faq/q11.17.html>

#define Y one
#define Z "one"

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

#define STR_aux(x) #x
#define STR(x) STR_aux(x)

int main(void)
{
printf("%s\n", STR_aux(Y));
printf("%s\n", STR(Y));
printf("%s\n", STR_aux(Z));
printf("%s\n", STR(Z));
printf("%s\n", Z);
return 0;
}

[output]
Y
one
Z
"one"
one
--
Martin Ambuhl

Nov 13 '05 #3

"Kelly" <ke***@invalid.com> wrote in message
news:3F**************@invalid.com...
Hi,

Ashamed to ask basic question but I cannot get this code to run,

#define Y one

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

int main (void) {
//Y=malloc(sizeof(char *));
printf ("%s",Y);

return 0;

}

I can compile but not run, gives me "the memory cannot be read".
All I want is to printf Y.

Please help, I know this is easy for most of you.


Try #define Y "one"

--
Jeff
Nov 13 '05 #4

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

Similar topics

5
by: DrUg13 | last post by:
#ifndef HEADER_H #define HEADER_H blah blal #endif So this tells the compiler That if its defined do not define it again. Could someone help me understand this. Does this mean that if I...
97
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
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 .
3
by: bamapookie | last post by:
I am new to VB, but not new to programming. I am using VB.Net 2003 and I have written a small app to monitor several running processes and everything is fine except the memory usage. When the app...
21
by: Philipp | last post by:
Hey, did anyone have a good paper about the opject orianteted concept? wishes
3
by: A_StClaire_ | last post by:
hi, not sure what I'm doing wrong here. getting "error C2064: term does not evaluate to a function taking 2 arguments" in response to my SortCardVector function...? Card.h: #pragma once
4
by: Mohammad Omer Nasir | last post by:
I was read Linux kernel code in which I saw few define macro defines in functions. The snap of the function is following and file name "err_ev6.c". static int ev6_parse_cbox(u64 c_addr, u64...
71
by: David T. Ashley | last post by:
Where is the best place to define TRUE and FALSE? Are they in any of the standard include files, ever? Do any standards apply? What I've traditionally done is something like: #ifndef...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.