Sunday, August 2, 2009

Question about command line arguments for C/C++, argv?

int main (int argc, char* argv[])





ok in this case,,, argv[] is supposed to be an array of pointers pointing to input





why is it possible to say


puts(argv[1])? //putstring





if argv is an array of pointers, wouldn't that print a memory address? but why then does it actually print the actual argument passed.





Don't we need to dereference it first... like


puts(*argv[1])?





I would greatly appreciate your help. Thanks!

Question about command line arguments for C/C++, argv?
You are correct in your understanding that argv is an array of pointers. So argv[1] is just a pointer. There are two critical insights to understanding this.





Critical insight #1: In C and C++, a pointer can be used as an array. So if you have a char*, that can point to either a single character, or to an array. Because of this, if you have "char *a;", the statements "*a" and "a[0]" are exactly the same thing. (Of course, you can't say "a[1]" if a is just a pointer to a single character. The results will be undefined.)





Critical insight #2: In C, strings (referred to in C++ as "C-style strings" to distinguish them from C++'s std::string class) are just arrays of characters. And when you pass a string (that is, a "char*") to puts(), the string itself is printed out, not its address.





Thus, if you say


char *foo = "bar"; puts(foo);


you will see "bar" as the program's output. Likewise, if you say


char *foo = argv[1]; puts(foo);


you will see the second argument to the program. (Why the second? Because argv[0] is the first argument.)





Something important to remember is that puts() will only print out strings. If you say "puts(3)" or "puts(argv)" your code won't compile. puts() always does the dereferencing on its own.
Reply:argv[1] stores address but *argv[1] gives the value stored at argv[1] address
Reply:It's not "an array of pointers pointing to input". It's actually a vector (or array) of char* (or strings) so each element in the vector is a null terminated string.





argv[0] is the program name


argv[1] is the first parameter passed to the program (separated by spaces)


and so on.





Hope that helps!!


I have an interview with office depot need some pointers?

i have an interview coming up i have went to school for a year to become a medical assistant i graduated about 5 mths ago i never took my certification test i had already had a call center job availble and had just got my apt so i needed a job quick to pay bills i just stayed at that job until i got terminated last month for attendance so on my application i just listed my last two jobs and it looks as if i didnt work since last year which is fine b/c i was in school and also having transportation issues but i dont know what to tell the interviewer about the field i graduated in he's going to want to know why im not working in my field truth is i dont feel confident enough to work in it yet i went to a 1yr college and i am going back to school to be an RN but i want to maybe sometime next year get a medical assistant job i just need work right now what should i tell him about that?

I have an interview with office depot need some pointers?
Most companies unless they have a huge turnover and don't mind (like McDonald's, Burger King, supermarkets) will not want to hire someone that they will have to spend time to teach only to leave and have to retrain someone else.





You may mention that you are attending school part-time or taking time off from school to work for awhile, but if they're not especially desparate to hire, or if there are many other people out there vying for the same position, I would not mention school.


C/C++ - nested objects/structs question?

I have a great idea of how to implement an algebra solving/simplfying program, but I need it to have a structure within itself. Here's what I mean:





struct thing {


thing *t1;


thing *t2;


int other;


};





As you can see, the struct uses itself, and therefore cannot define 'thing' until it is finished, making a sort of catch 22. Is there a way to do something like this? All it needs to do is store two pointers to other instances of the same data type and one integer. If it is possible with a class, that would work too.





The code can be completely different, as long as it does what I described or gives an alternate method.

C/C++ - nested objects/structs question?
Well my C is a bit rusty but I know it is officially impossible to declare a structure containing an instance of itself....talk about endless loop! so doubt C++ would allow it either as physically impossible.





Couldn't you make a small array to hold the pointers and int? you could use an ordinary do-while to loop through?
Reply:Pointers to similar structures or classes is a common concept in data structures. This is how things such as linked lists or trees. They have a structure with a pointer the same structure for the next one in the list or the children in the tree. Look up linked list in wikipedia to see what I mean.





Anyways, here is some code demonstrating what you want to do. This is in C, but you can do similar things with classes in C++. I also played with the variables a bit to demonstrate different ways of accessing the data.





#include %26lt;stdio.h%26gt;





//get the compiler to recognize Thing as its own data type





typedef struct Thing Thing;





//define the struct





struct Thing {


Thing *first;


Thing *second;


int val;


};





int main()


{


Thing o1;


Thing o2;


Thing o3;





Thing *p4;


Thing *p5;


Thing *p6;





//dynamicall created Things


p4 = (Thing*)malloc(sizeof(Thing));


p5 = (Thing*)malloc(sizeof(Thing));


p6 = (Thing*)malloc(sizeof(Thing));





o1.first = %26amp;o2;


o1.second = %26amp;o3;





o1.val = 1;


o2.val = 2;


o1.second-%26gt;val = 3; // does same as o3.val = 3





p4-%26gt;first = p5;


p4-%26gt;second = p6;





p4-%26gt;val = 4;


p5-%26gt;val = 5;


p4-%26gt;second-%26gt;val = 6; // does same as p6-%26gt;val = 6;





printf("%d\n", o1.val);


printf("%d\n", o1.first-%26gt;val);//same as printing o2.val


printf("%d\n", o3.val);





printf("%d\n", p4-%26gt;val);


printf("%d\n", p4-%26gt;first-%26gt;val); //same as printing p5-%26gt;val


printf("%d\n", p6-%26gt;val);





//free dynamically created objects


free(p4);


free(p5);


free(p6);





return 0;


}
Reply:Use forward class/struct declaration, for example:





struct thing;





struct thing {


thing *t1;


thing *t2;


int other;


};
Reply:first thought : try the 'this' pointer in a class .. might work as such





#include %26lt;iostream%26gt;


using namespace std;


class many


{


public:


int integer1;


many *ptr;





many()


{


integer1 = 0;


ptr = this;


}


};





void main()


{


many o;


cout%26lt;%26lt;o.integer1%26lt;%26lt;endl;





cout%26lt;%26lt;o.ptr-%26gt;integer1%26lt;%26lt;endl;


}


C/C++: Creating self-sufficient executables?

Hi!





I've tried to search online but I guess I don't have to right keywords in mind to find a satisfying result.





I have seen in the past software which seems to be self sufficient-- and by that I mean that every external library is included in the executable binary itself, so there are a few DLL's (besides the vital ones like mscrt, etc) needed by that program.





Considering I already have the source code of a certain library, is it possible to have it compiled and included in my final executable, rather than being a standalone DLL?





I dont need a in-depth actual explaination, but rather a few pointers (links, articles, manpages) to go into the right direction.





Thanks :)

C/C++: Creating self-sufficient executables?
Yes. DLLs are Dynamic Linking Libraries You want static libraries. Do a search on how to create (and use) static libaries for Windows with your compiler.
Reply:I can't tell you exactly because it depends on what compiler you are using and I don't remember exactly, but what you are looking for is how to compile without dependencies.





For certain compilers and frameworks (usually the microsoft ones) sometimes there will be DLL files you have no choice but distributing/using.

strawberry

C programming: strcpy?

does strcpy make an actual copy of a string or just a pointer to the string that you are trying to copy? i'm writing a program where i pass a string into a method and the method uses strtok on the string i pass. however strtok modifies the string, which i don't want it to do. i guess i'm having trouble with my pointers and i thought strcpy might fix it but i'm still having the same problem. any suggestions on how to stop the method from modifying the string?

C programming: strcpy?
strcpy does make a deep copy of a c string. In fact, it copies the original string into the destination buffer, without much checking. For more on strcpy, look at http://www.cplusplus.com/reference/clibr...





As for processing your string you have 2 options. One is to make a copy of the original strign and use strtok on the modified string, like you attempted. Another alternative is to implement, from scrath, the parsing functionality you desire, without modifying the original string.





PS: For security reasons, depending on what this code is for, I would not use strcpy. strncpy and even memcpy are safer.


C++ help plz?

i want to write a function that get the size of an array by using pointers , where i should make a pointer points at begin of the array and another one points at the end of the array , then i have to return the size of the array at last .





int size(double *begin,double *end), any help?

C++ help plz?
The previous answer does well to explain the idea of computing the difference in bytes between two addresses, and dividing by sizeof(double) to get the number of elements. It's good to understand that, but pointer arithmetic can simplify it greatly. This also works:





int size(double *begin, double *end) {


return end - begin + 1;


}





Assuming end points to the last element of the array. The size of what begin and end point to is built into the calculation, you get it for free.
Reply:C++ stores arrays by allocating a single continuous block of memory. So if you allocate ten doubles, you allocate a block of memory:


10 doubles * (8 bytes / double) = 80 bytes





Now the pointer gives you back the location of the first byte, so how you compute the size depends on exactly what "end" means.





There are actually several possibilities:





1.) If end is the location of the last byte, then for four doubles:





begin -%26gt; xxxxxxxx xxxxxxx xxxxxxxx xxxxxxxx %26lt;- end





The answer is ((end+1) - begin) / 8.





2.) If end is the location of the last double in the array, then for four doubles:





begin -%26gt; xxxxxxxx xxxxxxxx xxxxxxxx end-%26gt; xxxxxxxx





The answer is ((end - begin) / 8) + 1.





3.) If end is the location of the first byte after the array, then for four doubles:





begin -%26gt; xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx end -%26gt; x





This is the easiest case. The answer is (end - begin) / 8.





Imagine allocating a single double starting at location zero. Then you allocate bytes at address zero, one, two, ..., seven.





So if we allocate n objects, each of size s, then we allocate n*s bytes. If the first allocated address is f, then we allocate the following locations.





first = f + 0


last = f + n*s - 1





Hope this helps!


Help! I'm going to Small Claims Court Next Month Need Pointers...PLEASE!!?

Well technically I'm not going, but my friend is...She's taking her ex land lord to court b/c he refuses to give her back her deposit. She left the apartment in good condition even took several pictures and video of how the apartment was left before and after she moved out. Anyway, she never signed a contract just filled out and application. She even gave her landlord a 30 day moving notice...However, her landlord refuses to give her back her deposit b/c she was only living in the apartment less than 6 months so supposedly she forfeits the deposit?! I've never herd of this rule b4. That's why she's taking him to small claims next month... So do you think she has a chance of getting her money back?!? Should she take the pics to court? How about the video? Should she take it on a disk or show him from her camera? Your feed back is greatly appreciated....Thanks in advance...Best answers gets 10 points for sure!!! ;]

Help! I'm going to Small Claims Court Next Month Need Pointers...PLEASE!!?
Your friend is taking the correct steps to recover her deposit. The fact that she has proof of the condition prior and after is a good. I suggest that your friend prepare a written demand for the deposit with a written response on why the deposit is considered forfeited.





In small claims court, almost everything is admissible. It is more like peoples court you might see on TV.





The only thing I can see that would give the landlord cause to keep the deposit if there was a lease in place and the term of the lease was not upheld. The tenant is responsible for the remainder of the term until the unit is re-rented for the remainder of the term. Breaking the lease prior to the expiration of the term invokes this clause. Since there was no lease signed, the tenancy is considered month to month.





I assume that a move out inspection was conducted with the landlord and the landlord provided a clean report. Otherwise that would be the only area your friend is vulnerable. Technically (and if the landlord is unscrupulous) he could generate a report indication repairs that were needed that cant be shown in pictures or a video.





There are good and bad landlords out there. I personally am never offended when tenants ask for written correspondences from me. In fact I prefer to keep all communications between myself and my tenants in writing so that there is no misunderstanding.
Reply:10 Points LOL big dealllllll The question is how long she agreed to stay in the unit. and where it is located.





If she is college student in a College town, timing is a big deal





ALL my leases state plain and clear. "Moving out early forfeits any and all deposits, and prepaid rents".





I think that is pretty well standard in most leases..





She made an agreement, so she is in the wrong, Only thing in her favor is she can dispute the length of the lease agreed to... and fact she has no lease is something else, but if she signed application, might have some of the terms in it.





I have a young man now that is trying to move out after nine months because he is going to be gone this summer, and does not want to pay rent... But his lease says he has to pay, and even my web site says same thing... He will not get his deposit back, or any prepaid rent. Sometimes a landlord has to be a JERK, and this is one of them in my book, Else what good is a lease??
Reply:You're correct, she doesn't "forfeit" the deposit just because she was there less six months. I'm a landlord myself and I've never heard of that "law."





This assumes she doesn't have a lease. If she doesn't, she in effect has a month-to-month lease and can move out at any time, simply by giving a 30-day notice.





Note that in many states, her landlord actually owes her interest on her deposit too. Remember, it's her money... it's just a "deposit." Technically, many states can force a landlord to not only return the deposit, but the appropriate amount of interest as well. (It will be a very small amount, I'm sure. I wouldn't worry about this.)





An landlord can withhold a fair amount of the deposit for cleaning, to repair broken items, things like that. It sounds as if the judge will decide what is a fair amount. Her having pictures of the place after she left it, will help her. I don't know if the judge is going to want to look at a video. (Maybe.) So yes, definitely take the pictures to court.





How did she pay her landlord for this deposit? With a check? If so, can she prove it and can she prove how much she paid? Did the landlord give her a receipt? Have her bring all of these types of receipts too.





I can't believe she didn't sign any kind of a lease, though.
Reply:Have her check with the clerk or the judge's secretary regarding the admissability of the pictures and video. She should definitely bring them unless she is specifically told by the court or judge not to. I would have the pictures printed out and have 2 or 3 copies on disk (1 for landlord, 1 for court/judge). Same with the video.