LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-04-2007, 01:55 AM   #16
daYz
Member
 
Registered: Nov 2003
Distribution: Debian
Posts: 164

Original Poster
Rep: Reputation: 30

Sorry for the late response, I had some things to do.

Code:
 char out[256];
 for(int i = 0; i < strlen(argv[1]); i++)
   out[i] = argv[1][i];
This code works perfectly


This seems to go fine:

Code:
char var[10];
strcpy(var, *(argv+1));
When I compare the variable var with another variable the result is always false though, even when the strings are equal.

Code:
     char aap[3] = "aap";
     
     if ( strcmp(aap, var) == 0 ){

          char y = 'y';
          printf("%c", y);}

          else{

          char n ='n';
          printf("%c", n);}

          return 0;}
 
Old 03-04-2007, 02:16 AM   #17
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

Rep: Reputation: 147Reputation: 147
You didn't allow the string app to have any space for the null terminator... try
Code:
char app[4] = 'aap';
 
Old 03-04-2007, 02:31 AM   #18
daYz
Member
 
Registered: Nov 2003
Distribution: Debian
Posts: 164

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by graemef
You didn't allow the string app to have any space for the null terminator... try
Code:
char app[4] = 'aap';
Woohoo! That was the problem indeed. Thanks.

And be carefull over there please.


Quote:
Keep it simple: as simple as possible, but no simpler.
lol

Last edited by daYz; 03-04-2007 at 02:38 AM.
 
Old 03-04-2007, 02:56 AM   #19
daYz
Member
 
Registered: Nov 2003
Distribution: Debian
Posts: 164

Original Poster
Rep: Reputation: 30
The last thing I don't understand is the memset part from this:

Code:
arg_length = strlen( argv[i] );
arg_copy = malloc( ( arg_length + 1 ) * sizeof( char ) );
memset( arg_copy, 0, (arg_length + 1) * sizeof( char ) );
strncpy( arg_copy, argv[i], arg_length + 1 );

/* Do other stuff... */

free( arg_copy );
arg_length = 0;
Why is the memset part used here? To me it looks like that malloc already did the nessesary work.
 
Old 03-04-2007, 03:07 AM   #20
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

Rep: Reputation: 147Reputation: 147
It is used to inilisalise the area of memory. When malloc is used it will return a memory location and it could hold anything. This just ensures that the memory is clean.
 
Old 03-04-2007, 04:10 AM   #21
daYz
Member
 
Registered: Nov 2003
Distribution: Debian
Posts: 164

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by graemef
It is used to inilisalise the area of memory. When malloc is used it will return a memory location and it could hold anything. This just ensures that the memory is clean.
Thanks graemef.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
howto allow strings in header (.h) files? RHLinuxGUY Programming 1 05-20-2006 09:07 PM
Shell script to compare blocks of strings? bruno buys Programming 10 04-15-2006 02:16 PM
[C]How to compare two strings w/o using strcmp() kponenation Programming 22 11-23-2005 08:29 AM
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
How to compare these two strings in one line code? powerplane Programming 4 07-10-2003 12:09 AM


All times are GMT -5. The time now is 09:29 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration