just ME
just ME

Reputation: 1827

Wrong result when copying an array of strings

I have the following code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* user;
char* passwd;
int nr;

void test()
{
    int i=0;
    for(i=0;i<argc;i++)
    printf("Hello %s \n",user);
}

int main(int argc,char*argv[])
{
    int i;
    nr=argc;
    for (i=0; i<argc; i++)
    {
        user=strdup(argv[i]);

    }

    test();
return 0;
}

The result is the argv[argc] on all the positions. How can I fix this? I wwant to have that test() outside the loop.

**

EDIT

** After the ANSWERS here this is my new code, which is not working. Can anyone say why?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* user;


void test(int n)
{
    int i=0;
    for(i=0;i<n;i++)
    printf("%s \n",user[i]);
}
int main(int argc,char*argv[])
{
     user = (char*) malloc(argc*sizeof(char));
int i;
for (i=0;i<argc;i++)
{
user[i]=argv[i];
}
test(argc);
return 0;
}

Upvotes: 0

Views: 77

Answers (4)

P.P
P.P

Reputation: 121357

Because you overwrite the pointers user and passwd in every iteration. Hence, you'll only see the last string.

If you can tell your aim of the program, a better answer can be provided. Because I am not sure whether you want to read one user and passwd Or an array of users and passwds.

After you edit, I see you want to read an array of strings:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char** user;
// or char *user[100]; /* If you want a fix length array of pointers. Now, you dont have to malloc. /*
char* passwd;
int nr;

void test(int argc)
{
    int i=0;
    for(i=0;i<argc;i++)
    printf("Hello %s \n",user[i]);
}

int main(int argc,char*argv[])
{
    int i;
    nr=argc;
    user = malloc(argc*sizeof(char*));

    for (i=0; i<argc; i++)
    {
        user[i]=strdup(argv[i]);

    }
    test(argc);
return 0;
}

Upvotes: 0

guest
guest

Reputation: 11

$ cat trash.c
#include <stdio.h>
#include <string.h>

void test(FILE* stream, char* usr, char* pass) {
    fprintf( stream, "%s@%s\n", usr, pass);
}

int main(int argc, char** argv) {

    int i = 1;
    if (argc % 2) {

        while(argv[i]) {

            test(stdout, argv[i], argv[i + 1]);
            i += 2;
        }
    }
    return 0;
}

$ clang trash.c
$ ./a.out user1 pass1 user2 pass2
user1@pass1
user2@pass2
$

also if you call strdup() don't forget to free memory, because strdup called malloc().

Upvotes: 0

perreal
perreal

Reputation: 97938

You are assigning to both password and user at each iteration of the for loop. The final values you see are from the last iteration. Also, there is memory leak due to overwriting the pointers from previous strdup calls. In fact, you do not need a loop:

int main(int argc,char*argv[])
{
  if(argc == 3) {
    user=strdup(argv[1]);
    passwd=strdup(argv[2]);
  } else {
    // error: usage
  }
  test();
  return 0;
}

If you want to have multiple user/password combinations:

char *user[256], *passwd[256]; 

void test(int n) {
  int i;
  for(i=0;i<n;i++)
    printf("Hello %s \n",user[i]);
}

int main(int argc,char*argv[])
{
  int i;
  for(i = 0; i < argc && i < 256; i+=2) {
    user[i]=strdup(argv[i]);
    passwd[i]=strdup(argv[i+1]);
  } 
  test(argc);
  return 0;
}

Upvotes: 1

Jens
Jens

Reputation: 72639

Of course; in test() you don't use i other than a loop variable and in main() you keep overwriting the previous value of user and passwd. In effect, what you do is:

user   = strdup(argv[0]);  /* Note: argv[0] is the program name. */
passwd = strdup(argv[0]);
user   = strdup(argv[1]);
passwd = strdup(argv[1]);
user   = strdup(argv[2]);
passwd = strdup(argv[2]);
user   = strdup(argv[3]);
passwd = strdup(argv[3]);
printf("%s %s \n", user, passwd);

With this information, can you fix your program?

Upvotes: 0

Related Questions