0

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;
}
3
  • Re edit: You need malloc(argc*sizeof(char*)). You want an array of char*, but forgot the * in the sizeof argument. And you must declare user as a char**. Commented Sep 20, 2012 at 12:34
  • why do I neec char* and char**?! can you please explain? I am confused Commented Sep 20, 2012 at 12:42
  • argv[i] is a pointer to char. You (try to) assign that to user[i], for that to work, user[i] must be compatible with char*, so user should be either an array of char* - like char *user[100]; if the size is known beforehand - or a pointer to a block of memory used as an array of char*. The type of thing that user points to must then have the size of a char* for the indexing to work properly. The things pointed to by user must have the size of char*, and they're used as char*, so the proper thing is to declare them as char*, and user as a pointer to char*. Commented Sep 20, 2012 at 12:49

4 Answers 4

1

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

perhaps you need another for, not this one.
I ve also edited my code. I don t know the array dimmension so I am using a dynamic array. Can you please tell me what am I doing wrong in the code I ve post under EDIT? PLEASE?
0

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;
}

5 Comments

@justAngela see my edit now. Note that argc is not visible in test() and your code won't compile due to that.
i ve also edit my post, but mine is not working case I use char* instead of char**. Can you tell me why do YOU USE CHAR** instead of char* when initializing the array? THank you
char* is just pointer to a char, meaning you can only store one null-terminated string. Whereas char** is to store an array of strings which is what you want here.
also why didn t you add malloc(argcsizeof(char))? Can you please explan? THANK YOU VERY MUCH!
I edited. You can use either malloc or a fixed length like char *user[100];
0

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?

2 Comments

i see. so i need an array of users and passwords,no? I ve edited my code. Can you help me to solve it,please?
I'm not sure what you want to do. Please specify an example of the program invocation with arguments, and what you expect the output to look like exactly.
0
$ 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().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.