Solved

string problems

Posted on 1998-08-26
5
201 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Grammars for C C++ and java 1 139
fatal error: vector: No such file or directory   Visual Studio 2015 for Arduino 4 105
How do i run a c++ file? 15 52
visual C++ 1 9
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

726 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