C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 24th, 2015, 01:02 AM
juzuze juzuze is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2015
Posts: 1 juzuze User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 44 sec
Reputation Power: 0
Input directory as command line arguement problem

hey all.

i am building a program that takes two directories from the command line, and have not been able to get it to work in this way. i have tried hard coding the directories, and have gotten that working just fine, but not when i enter through command line.

the code that i have used for hard coding is:

char SrcPath[] = "home/user/Desktop/cat1"; // source path
char DestPath[] = "home/user/Desktop/cat2"; // destination path

i have attempted using

char SrcPath[] = argv[1];

and that didn't work, also tried

char * SrcPath[] = argv[1];

and that doesn't seem to work either.

can somebody please explain why, and help me with this problem?
thank you in advance,

Reply With Quote
  #2  
Old May 14th, 2015, 02:03 AM
Thinias Thinias is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2010
Posts: 78 Thinias User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 h 58 m 48 sec
Reputation Power: 8
Code:
char * SrcPath[] = argv[1];


This represents a misunderstanding of what char* means. In most cases, a char* and a char[] are functionally the same thing. These two lines produce nearly identical objects:
Code:
char* srcPath = new char[100];
char srcPath[100];


These two objects are functionally identical. What does this mean? Throw the following dummy program in your compiler and watch it compile successfully:

Code:
int test(char* test)
{
	return 0;
}

int main()
{
	char* testPointer = new char[100];
	char testArray[100];

	test(testPointer);
	test(testArray);

	return 0;
}

This is what I mean by "functionally identical". Anywhere I can pass in a char*, I can also pass in a char[], and in this case both arrays have length 100.

With this in mind (the fact that char* and char[] are functionally equivalent), take another look at this line and see if you can understand why it doesn't compile:

Code:
char * SrcPath[] = argv[1];



As an unrelated sidebar... the primary difference between these two implementations is in the form of memory management. In the former case, you end up with a pointer to a character array on the stack, whereas the character array itself is allocated on the heap; because you allocated it on the heap, you are also responsible for manually delete[]-ing it when you're done. In the latter case, the entire character array is allocated on the stack, and will be released automatically for you when the function goes out of scope. There's also a semantic difference in that the former implementation can define the size of the array at runtime, whereas the latter must be hard-coded at compile time.

Good luck!
-Thinias

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Input directory as command line arguement problem


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.

© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap