473,385 Members | 1,748 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.

question about vc++.net compiler

#include <stdio.h>

#include <string.h>
int main(void)
{
char message[]="Welcome to C Programming!";
char *cptr;
cptr = message + 5;
printf("%c\n", cptr);
return 0;
}

the above code, when i put it in VC++.NET, it can't display properly.
but when i put it in MinGW Developer Studio, it can show the result 'm'.
why?

thanks!
Nov 18 '05 #1
4 1045
A.A
printf("%s\n", cptr);
or printf("%c\n", *cptr);

Nov 18 '05 #2
A.A
printf("%s\n", cptr);
or printf("%c\n", *cptr);

Nov 18 '05 #3

"nick" <i1********@yahoo.com> wrote in message
news:dl**********@justice.itsc.cuhk.edu.hk...
#include <stdio.h>

#include <string.h>
int main(void)
{
char message[]="Welcome to C Programming!";
char *cptr;
cptr = message + 5;
printf("%c\n", cptr);
return 0;
}

the above code, when i put it in VC++.NET, it can't display properly.
There is no 'properly'. The behavior is undefined.
%c is for type 'char' not 'char*'.

Perhaps you meant:
printf("%c\n", *cptr);
but when i put it in MinGW Developer Studio, it can show the result 'm'.
why?


Undefined behavior can be manifest as anything.

-Mike
Nov 18 '05 #4
On Fri, 18 Nov 2005 13:00:09 +0800, in comp.lang.c , nick
<i1********@yahoo.com> wrote:
#include <stdio.h>

#include <string.h>
int main(void)
{
char message[]="Welcome to C Programming!";
char *cptr;
cptr = message + 5;
printf("%c\n", cptr);
return 0;
}

the above code, when i put it in VC++.NET, it can't display properly.
but when i put it in MinGW Developer Studio, it can show the result 'm'.
why?


%c prints a character. The 5th character of message is 'm' which is
presumably what you were expecting.

However cptr is a pointer. Printing the pointer with %c is undefined
behaviour, and the result could be anything (often it will be the
value of the address, converted to a char). So, if you got 'm' on
Mingw, it was coincidence.

Change the printf to *cptr, to get the value stored at cptr.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 19 '05 #5

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

Similar topics

65
by: perseus | last post by:
I think that everyone who told me that my question is irrelevant, in particular Mr. David White, is being absolutely ridiculous. Obviously, most of you up here behave like the owners of the C++...
13
by: Jason Swett | last post by:
I want to do graphics with C++. Surprisingly, so far nobody has been able to tell me anything helpful. How do I do it? Any input would be greatly appreciated. Jason
5
by: Wajih-ur-Rehman | last post by:
The question is about C++ (since its the C family, i posted it on this newsgroup) Lets say i declare an array int a = {1,2,3,4}; int *p = a; //This is allowed because "a" returns the address of...
4
by: Alex | last post by:
Hi there I'm switching from VC++ 6.0 to VC++ .NET 2003. Since there is no stand-alone version of VC++ .NET 2003 Pro, I went and purchased the Standard version, which does not have an...
3
by: kaizen | last post by:
Hi, we have the C/C++ code compiled in 16-bit compiler under DOS. assume it as library. This library should be used to write code in VC++ and should be compiled in 32-bit compiler under...
8
by: Abubakar | last post by:
Hi, I am writing some unmanaged c++ code that would need to be compiled in the future using compilers other than vc++. I'm using the feature of vc++ "use precompiled headers", is there going to...
6
by: Malvolio | last post by:
I know this is a *really* stupid question but what the hell is visual c++? I downloaded it and it doesn't seem to work. It isn't like any c++ compiler I ever saw before and the manual on the...
6
by: JDT | last post by:
Hi, I am curious if there is a way to just use existing STL functions, algorithms etc to accomplish the following loop (without a need to write my own function). I know how to use some STL...
2
by: ambarish.mitra | last post by:
#include <iostream> class base { public: virtual void f() {} }; class derived: public virtual base { }; int main()
8
by: =?ISO-8859-1?Q?Konrad_M=FChler?= | last post by:
Hi, I've a list of objects. I iterate the list and read the value of each object for many operation like: x = myList.value1 + myList.value2 etc. My question: Is it efficient to always use...
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...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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:
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 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.