![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5
![]() |
i've made my own copy of the tool "echo", this is not to improve it or anything, but only to learn about strings and pointers...
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
int main(int argc,char** argv[])
{
if(argc>1)
{
char *str[100];/*one array for every argument*/
char *str2=" ";
int loop=0;
int count=1;
while (loop<=argc)/*loop to copy every argument to a string[]*/
{
str[loop]=argv[count];
loop++;
count++;
}
for(loop=0;loop<argc-1;loop++)/*loop to print the strings with a space between them(just like they were entered)*/
{
printf("%s",str[loop]);
printf("%s",str2);
}
return 0;
}
return EXIT_FAILURE;
}
__________________
http://www.white-scorpion.nl |
|
|
|
|
|
#2 | |
|
Programmer
|
Quote:
#include <stdio.h>
int main(int argc, char *argv[]){
int i;
for(i = 1;i < argc;i++){
printf("%s", argv[i]);
}
printf("\n");
}
__________________
ALLOW IMAGES IN SIGNATURES NOW |
|
|
|
|
|
|
#3 | |
|
Hobbyist Programmer
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5
![]() |
Quote:
only thing: printf("%s", argv[i]); i would change into : printf("%s ", argv[i]); cause then the problem of the space is solved... great, guess i didn't had to make it this difficult ![]()
__________________
http://www.white-scorpion.nl |
|
|
|
|
|
|
#4 |
|
Expert Programmer
|
Since no one else fielded the post increment operators on loop and count
count++; loop++; I will field that. ![]() As it ends up post increment operators are actually very inefficient. You chould have used the following code: while (loop<=argc)/*loop to copy every argument to a string[]*/
{
str[loop++]=argv[count++];
}If you are using a post-increment operator on an operand in which the pre-incremented value is no being used in the same line, then you are wasting memory and time since a post increment operator will actually instantiate a copy of the variable, increment the original and return the copy.. kind of like this: (I will use a template so that I can demonstrate the generic use of the operator when showing my point) //post increment operator...
template< classname typeT >
typeT operator++( typeT &lhv, typeT )
{
typeT temp = lhv;
lhv += 1;
return temp;
}
//preincrement operator
template< classname typeT >
typeT &operator++( typeT &rhv )
{
rhv += 1;
return rhv;
}Also as you can see due to the nature of the post and pre increment operators a second parameter (which fortunatly does not have to even be initialized) must be present for a post increment.
__________________
Clifford Matthew Roche <geek@cliffordroche.com> Web Hosting: http://www.crd-hosting.com Consulting: http://www.crdev-consulting.com |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5
![]() |
see, there's the difference between me (newbie) and you (proffesional programmer), i don't understand what the f**k you are talking about
![]() but eventually i will, i just have to practice/learn and be patient. ( i just started programming 2 months ago )
__________________
http://www.white-scorpion.nl |
|
|
|
|
|
#6 |
|
Expert Programmer
|
I fyou do a google such for the C++ FAQ Lite, you will actually find that point, and a few other very good points covered which touch on topics of robustness and optimization, perhaps it could explain that a little bit more.
You will eventually though come to a point where you will consider all points such as that when tackling a project No worries..
__________________
Clifford Matthew Roche <geek@cliffordroche.com> Web Hosting: http://www.crd-hosting.com Consulting: http://www.crdev-consulting.com |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5
![]() |
thanks, i will do that
![]()
__________________
http://www.white-scorpion.nl |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|