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

help with Char pointers (for formatted dates)

Hello -

I'm re-learning some programming to use at work (haven't done C in 11 years and am learning C++)...

I'm returning a pointer from a function and when it's returned, the target char array has some extra garbage characters at the end.

So here's the function I've written (with much stripped away):

pMydate points to a 'char MyOtherArray[17]' outside of the function...

Expand|Select|Wrap|Line Numbers
  1. char* asciDate(char* pMydate)
  2. {
  3.    char  myarray[17];
  4.    char* pMyarray;
  5.  
  6.    // some code here that puts the characters in the string...
  7.  
  8.    pMyarray = myarray;
  9.    strncpy(pMydate, pMyarray, 17);
  10.    return pMydate;
  11. }
When I get back to the code that calls this function, MyOtherArray holds exactly what I expect. pMydate, however, which is pointing to MyOtherArray, shows the 17 characters I expect, then 2 additional garbage characters.

If I try to output pMydate to stdio or a file, it includes the garbage. (i.e. cout << pMydate;)

Why are 2 garbage characters showing up where the pointer is pointing?

Platform = WinXP
Using Microsoft Visual C++ (Visual Studio 8)
Nov 21 '07 #1
2 1050
oler1s
671 Expert 512MB
How is a regular character array different from a C string? Remember that a C string requires that it be terminated with a NUL ('\0') character. If you have 17 letters, to form a C string, you need the 18th NUL character.
Nov 21 '07 #2
Thanks, figured it was a stupid newbie question!!!

Works like a charm.

Regards,

Travis
Nov 21 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: ArShAm | last post by:
Hi there Please help me to optimize this code for speed I added /O2 to compiler settings I added /Oe to compiler settings for accepting register type request , but it seems that is not allowed...
7
by: stephane | last post by:
Hi, I must implement the method printLn of the class Thoraire. And I've done this, can someone tell me if I am right ?
1
by: antsays | last post by:
When I run a sourceSafe command like history directly on my server it returns results with Great Britain formatted dates. But when the Webserver which is running on the same server runs the same...
4
by: Steve Chow | last post by:
I was wondering if someone could help with my assignment. I've pretty done finished the work but cannot figure out why it only prints the last one added. Here is my code. The way things are done...
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: 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
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...
0
marktang
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,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
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,...

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.