Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

string problems

Posted on 1998-08-26
5
Medium Priority
?
213 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 

Author Comment

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

Accepted Solution

by:
nietod earned 150 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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 …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

597 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