Solved

string problems

Posted on 1998-08-26
5
194 Views
Last Modified: 2010-04-01
One last problem with my "getdiskspace" function...
The RootPathName has to hold the app path to give over to the api call, I've tried declaring the variable as a string but that caused problems, I thought the below code would work but it keeps coming up garbage. Code follows ->

char RootPathName[4];

RootPathName[0] = char(_getdrive() + 'A' -1);

RetVal = GetDiskFreeSpace(RootPathName,&SectorsPerCluster,
         &BytesPerSector,&NumberOfFreeClusters,  &TotalNumberOfClusters);
0
Comment
Question by:jtm082698
  • 3
  • 2
5 Comments
 

Author Comment

by:jtm082698
ID: 1171305
Edited text of question
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1171306
Your string is not termianted.  details follows.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1171307
C strings are arrays of characters that are terminated (end with) a NUL character.  When you declare

char RootPathName[4];

You have an array of 4 random characters.  This is followed by more random bytes that are not really part of the array and may cause an error it they are accessed.   Thus this array does not necessarily represent a string of 3 or 4 characters.  The string begins with the first character and continues through randomly set characters until it happens to come across a NUL character.

When you do

RootPathName[0] = char(_getdrive() + 'A' -1);

you set the first entry in the array to a letter, but the following entries are still random, thus the array does not necessarily hold a NUL terminated string.  

If you want the string to hold only 1 character, you can set the array 2nd entry to NUL by doing

RootPathName[1] = '\0';
0
 

Author Comment

by:jtm082698
ID: 1171308
Okay, I'm a little confused...can't you set an array or string to a line of text (ex. RootName = "C:\\" or "Hi-Di-Ho Kyle" without the null character)
0
 
LVL 22

Expert Comment

by:nietod
ID: 1171309
Yes and no.  

(I guess you want more than that.)  first of all constant literal string, that is, 0 or more characters inside double quotes, like "C:\\", is always represented by the compiler as an array of characters followed by an extra NUL character that you didn't specify.  Thus "ABC" is a 4 character array with 4th character being a NUL.  "ABC\0" is a 5 character array with the 4th and 5th chracters both beng NUL.  So

RootName = "C:\\";

says rootname points to a 4 character array that contains a "C", a  ":", a "\" and then a NUL.  So RootName = "C:\\"; does set rootname to a valid nul terminated string.  Also

char RootName[4] = "C:\\";

does as well and is more like your original code.  But

char RootName[4];

leaves the 4 characters unitialized.  So it could contain a string that goes past the 4 characters allocated.

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question