Error passing argument from strcpy for usage statement

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1620 Views - Last Post: 11 March 2010 - 04:10 AM Rate Topic: -----

#1 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Error passing argument from strcpy for usage statement

Posted 07 March 2010 - 04:55 AM

Ok, this is going to show exactly how bad I am. I know this is simple but I'm just not getting it. I'm trying to pass arguements (one of which being the program name) and I'm getting a warning from gcc that argument 1 of strcpy is of an incompatible pointer type? This worked perfect until I through in some pointers for the args. Warnging: this is very terrible code produced by a long night and little sleep:

#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 

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

  int i;     
  int start; 
  int end;   
  char name[8];
  char child[BUFSIZ];
  char prog[BUFSIZ]; 

	
  //check for right number of arguments
  if(argc != 4)
  {
	fprintf(stderr, "Your doing it wrong :P/>                                  \nUsage: %s <program name> <start no.> <end no.>\n", name);
	exit(1);
  }
  else{
	char *name[BUFSIZ],  *prog[BUFSIZ];
	int *start, *end, *i;

	strcpy(name, "name");
	scanf("%s", "%s", "%d", "%d", &name, &prog, &start, &end);

	for(i = start; i < end+1; i++)	
	{
 
	printf ("Do it!\n");

	}

  }

  return 0;
}

This post has been edited by Israel: 07 March 2010 - 08:27 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Error passing argument from strcpy for usage statement

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Error passing argument from strcpy for usage statement

Posted 07 March 2010 - 05:09 AM

char *name[BUFSIZ];


This is a pointer to an array of pointers. Did you mean:
char name[BUFSIZ]; // array of BUFSIZ - 1 characters


EDIT:
fprintf(stderr, "Your doing it wrong :P/>  // >>>> Guess that's a copy-paste error?:)/>


and:
*prog[BUFSIZ];


should be:
prog[BUFSIZ];


This post has been edited by sarmanu: 07 March 2010 - 05:17 AM

Was This Post Helpful? 0
  • +
  • -

#3 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Error passing argument from strcpy for usage statement

Posted 07 March 2010 - 05:09 AM

argv needs to have an undefined number of indices. If it only has 1, then you'll only get a single argument -- the program's name. Here's a few ways to use the cmdline arguments:
int main(int argc, char *argv[]) {
    //code
    return 0;
}

Was This Post Helpful? 0
  • +
  • -

#4 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Re: Error passing argument from strcpy for usage statement

Posted 07 March 2010 - 05:27 AM

Quote

// >>>> Guess that's a copy-paste error?

Yes :) If you scroll to the far right you'll see what was left off. My bad.

Quote

argv needs to have an undefined number of indices. If it only has 1, then you'll only get a single argument -- the program's name. Here's a few ways to use the cmdline arguments:

From what I read here by No.2 Pencil [] is for 1-3 arguments and I have 4 which needs [1] to work.

Quote

This is a pointer to an array of pointers. Did you mean:

Nope, didn't want a pointer to a pointer. I was looking at this and thought I may need to use a pointer like they did. However its been a while since I've used them, or used them with char arrays. Kind of got lost to tell you the truth.
Was This Post Helpful? 0
  • +
  • -

#5 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Error passing argument from strcpy for usage statement

Posted 07 March 2010 - 05:30 AM

You misread it entirely.

He's saying that argv[1] is the first argument, not that argv[1] is required for 4 arguments.
Was This Post Helpful? 0
  • +
  • -

#6 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Re: Error passing argument from strcpy for usage statement

Posted 07 March 2010 - 05:37 AM

Quote

You misread it entirely.

Crap... Your right. That's what no sleep will do.

I should've put more parameters in main to define the args right?
Kind of like:
int main(int argc, argd, char *argv[] *argx[])

Was This Post Helpful? 0
  • +
  • -

#7 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Error passing argument from strcpy for usage statement

Posted 07 March 2010 - 05:41 AM

No.
//example
#include <stdio.h>

int main(int argc, char *argv[]) {
    for(int i = 1; i < argc; ++i) {
        printf("%s\n", argv[i]);
    }
    getchar();
    return 0;
}
That code prints all the arguments.

This post has been edited by PlasticineGuy: 07 March 2010 - 05:43 AM

Was This Post Helpful? 1
  • +
  • -

#8 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Re: Error passing argument from strcpy for usage statement

Posted 07 March 2010 - 05:55 AM

Ok, I get that now, thanks. But how do I send them to the variables like I tried with scanf? Do I need to make a function to pass them to?
Was This Post Helpful? 0
  • +
  • -

#9 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Error passing argument from strcpy for usage statement

Posted 07 March 2010 - 05:59 AM

No. Just set it:
char* name = argv[0], prog = argv[1]; //etc

Was This Post Helpful? 0
  • +
  • -

#10 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Re: Error passing argument from strcpy for usage statement

Posted 07 March 2010 - 06:05 AM

Ahhh! So simple, seems like the hardest things always are for me. Its now 8am where I live. I'm going to have to try this after a nap. Thanks!
Was This Post Helpful? 0
  • +
  • -

#11 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Error passing argument from strcpy for usage statement

Posted 07 March 2010 - 06:11 AM

By the way, let me tell you if you stick with C, you're in for some fun with subtle memory bugs.

This post has been edited by PlasticineGuy: 07 March 2010 - 06:12 AM

Was This Post Helpful? 0
  • +
  • -

#12 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Re: Error passing argument from strcpy for usage statement

Posted 07 March 2010 - 08:44 PM

Quote

By the way, let me tell you if you stick with C, you're in for some fun with subtle memory bugs.

I will :)

Still getting some warnings and a little odd behavior even though it compiles.
args.c: In function 'main':
args.c:26: warning: initialization makes integer from pointer without a cast
args.c:27: warning: initialization makes pointer from integer without a cast
args.c:32: warning: assignment makes integer from pointer without a cast



I know I probably did argc wrong. I assume thats what giving strcpy problems.
  if(argc != 4)
  {
	fprintf(stderr, "Your doing it wrong :P/>                                  \nUsage: %s <program name> <start no.> <end no.>\n", name);
	exit(1);
  }
  else{

	char* name = argv[0], prog = argv[1];
	int* start = argc, end = argc, i = argc;

	strcpy(name, "name");

	for(i = start; i < end+1; i++)	
	{
 
	printf ("Do it!\n");

	}

  }


Was This Post Helpful? 0
  • +
  • -

#13 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Error passing argument from strcpy for usage statement

Posted 07 March 2010 - 09:07 PM

Pay attention to your warnings!!!
prog = argv[1];

Assigning pointer to a char
int* start = argc,

Assigning integer to a pointer
for(i = start;

Assigning pointer to an int
Was This Post Helpful? 0
  • +
  • -

#14 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Re: Error passing argument from strcpy for usage statement

Posted 08 March 2010 - 02:35 PM

Quote

Assigning pointer to a char

Was trying to follow what PlasticineGuy did. I tried assigning it to argv instead, after your post but I get the same error?

Quote

Assigning integer to a pointer

Not sure how to get around that assignment...

Quote

Pay attention to your warnings!!!

warning: initialization makes pointer from integer without a cast

True. But that's what I'm struggling trying to remember.
Was This Post Helpful? 0
  • +
  • -

#15 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Error passing argument from strcpy for usage statement

Posted 08 March 2010 - 09:43 PM

Why did you make start a pointer? argc is a value, not a pointer. Look at the main prototype:
int main(int argc, char **argv); //not int *argc

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2