Newbie needs help correcting code

Z

zotkara

Hello. I need help correcting the following C source code.

/* coder.c */
/* Usage: coder [filename] [action]
[action]
D decrypt
C crypt
*/

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

#define TITLE "coder\nAbout: encrypts or decrypts file."
#define USAGE "Usage: coder [filename] [action]\n\t[action]\n\td\tDecrypt file\n\tc\tCrypt file"
#define FILE_ERROR -2
#define DECRYPT 'd'

void start(int argc,char argv[]);
int encode_character(int ch,int val);
int decode_character(int ch,int val);

typedef crypt
{
int rv; // Recovery value
int ch; // Channel
unsigned int ctr; // Counter
int val; // Encrypt value
char buffer[257]; // Buffer
FILE *fh; // File handle
}engine0={1,0,0,5};

int main(int argc,char *argv[])
{
start(argc,argv); // Start engine

}

void start(int argc,char argv[])
{
crypt *engine1=&engine0; // Turn on engine
if(argc!=3)
{
printf("%s\n%s\n",TITLE,USAGE);
}

/* Note that content of if(content) is case-sensitive */
else if(argv[2]==DECRYPT)
{
(engine1->fh)=fopen(argv[1],"r"); // Opens file
if((engine1->fh)<=0)
{
printf("\n\nError opening file...");
(engine1->rv)=FILE_ERROR;
}
}
}
EOF

The following is the error message output by GNU gcc compiler in Slackware
10.2 in bash.

error: syntax error before '{' token
coder.c:29: error: parse error before '}' token
coder.c:29: error: parse error before ',' token
coder.c:29: warning: excess elements in scalar initializer
coder.c:29: warning: (near initialization for `engine0')
docs@pyenos:~/work_and_play/c++_learn$ gcc coder.c -o coder
coder.c:22: error: syntax error before '{' token
coder.c:29: error: parse error before '}' token
coder.c:29: warning: excess elements in scalar initializer
coder.c:29: warning: (near initialization for `engine0')
coder.c:29: warning: excess elements in scalar initializer
coder.c:29: warning: (near initialization for `engine0')
coder.c:29: warning: excess elements in scalar initializer
coder.c:29: warning: (near initialization for `engine0')
coder.c:29: warning: data definition has no type or storage class
coder.c: In function `main':
coder.c:33: warning: passing arg 2 of `start' from incompatible pointer type
coder.c: In function `start':
coder.c:39: error: `crypt' undeclared (first use in this function)
coder.c:39: error: (Each undeclared identifier is reported only once
coder.c:39: error: for each function it appears in.)
coder.c:39: error: `engine1' undeclared (first use in this function)
coder.c:48: warning: passing arg 1 of `fopen' makes pointer from integer
EOF

Please help me correcting this code as I am a newbie. Thanks in advance.


Zot
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hello. I need help correcting the following C source code.

/* coder.c */
/* Usage: coder [filename] [action]
[action]
D decrypt
C crypt
*/

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

#define TITLE "coder\nAbout: encrypts or decrypts file."
#define USAGE "Usage: coder [filename] [action]\n\t[action]\n\td\tDecrypt file\n\tc\tCrypt file"
#define FILE_ERROR -2
#define DECRYPT 'd'

void start(int argc,char argv[]);
int encode_character(int ch,int val);
int decode_character(int ch,int val);

typedef crypt

what sort of type is crypt? Yes, you typedef'ed it, but what did you
typedef it to?
{
int rv; // Recovery value
int ch; // Channel
unsigned int ctr; // Counter
int val; // Encrypt value
char buffer[257]; // Buffer
FILE *fh; // File handle
}engine0={1,0,0,5};

[snip]

- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFDut5WagVFX4UWr64RArzUAJ91EcAdn+QYa3J4Z5sDGDjjMZdTWQCgv2+B
aMnGBLwDqYvcvgvsypv5v4s=
=Cmpy
-----END PGP SIGNATURE-----
 
M

Mike Wahler

zotkara said:
Hello. I need help correcting the following C source code.

/* coder.c */
/* Usage: coder [filename] [action]
[action]
D decrypt
C crypt
*/

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

#define TITLE "coder\nAbout: encrypts or decrypts file."
#define USAGE "Usage: coder [filename] [action]\n\t[action]\n\td\tDecrypt
file\n\tc\tCrypt file"
#define FILE_ERROR -2
#define DECRYPT 'd'

void start(int argc,char argv[]);
int encode_character(int ch,int val);
int decode_character(int ch,int val);

typedef crypt

typedef struct crypt
{
int rv; // Recovery value
int ch; // Channel
unsigned int ctr; // Counter
int val; // Encrypt value
char buffer[257]; // Buffer
FILE *fh; // File handle
}engine0={1,0,0,5};

int main(int argc,char *argv[])
{
start(argc,argv); // Start engine

}

void start(int argc,char argv[])
{
crypt *engine1=&engine0; // Turn on engine
if(argc!=3)
{
printf("%s\n%s\n",TITLE,USAGE);
}

/* Note that content of if(content) is case-sensitive */
else if(argv[2]==DECRYPT)
{
(engine1->fh)=fopen(argv[1],"r"); // Opens file
if((engine1->fh)<=0)
{
printf("\n\nError opening file...");
(engine1->rv)=FILE_ERROR;
}
}
}
EOF

The following is the error message output by GNU gcc compiler in Slackware
10.2 in bash.

error: syntax error before '{' token
coder.c:29: error: parse error before '}' token
coder.c:29: error: parse error before ',' token
coder.c:29: warning: excess elements in scalar initializer
coder.c:29: warning: (near initialization for `engine0')
docs@pyenos:~/work_and_play/c++_learn$ gcc coder.c -o coder
coder.c:22: error: syntax error before '{' token
coder.c:29: error: parse error before '}' token
coder.c:29: warning: excess elements in scalar initializer
coder.c:29: warning: (near initialization for `engine0')
coder.c:29: warning: excess elements in scalar initializer
coder.c:29: warning: (near initialization for `engine0')
coder.c:29: warning: excess elements in scalar initializer
coder.c:29: warning: (near initialization for `engine0')
coder.c:29: warning: data definition has no type or storage class
coder.c: In function `main':
coder.c:33: warning: passing arg 2 of `start' from incompatible pointer
type
coder.c: In function `start':
coder.c:39: error: `crypt' undeclared (first use in this function)
coder.c:39: error: (Each undeclared identifier is reported only once
coder.c:39: error: for each function it appears in.)
coder.c:39: error: `engine1' undeclared (first use in this function)
coder.c:48: warning: passing arg 1 of `fopen' makes pointer from integer
EOF

Please help me correcting this code as I am a newbie. Thanks in advance.

OK now it compiles. Whether it does what you want I have no idea.

/* coder.c */
/* Usage: coder [filename] [action]
[action]
D decrypt
C crypt
*/

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

#define TITLE "coder\nAbout: encrypts or decrypts file."
#define USAGE "Usage: coder [filename] [action]\n\t[action]\n\td\tDecrypt
file\n\tc\tCrypt file"
#define FILE_ERROR -2
#define DECRYPT 'd'

//void start(int argc,char argv[]);
void start(int argc,char *argv[]);
int encode_character(int ch,int val);
int decode_character(int ch,int val);

typedef struct
{
int rv; // Recovery value
int ch; // Channel
unsigned int ctr; // Counter
int val; // Encrypt value
char buffer[257]; // Buffer
FILE *fh; // File handle
} crypt ;

crypt engine0={1,0,0,5};

int main(int argc,char *argv[])
{
start(argc,argv); // Start engine

}

//void start(int argc,char argv[])
void start(int argc,char *argv[])

{
crypt *engine1=&engine0; // Turn on engine
if(argc!=3)
{
printf("%s\n%s\n",TITLE,USAGE);
}

/* Note that content of if(content) is case-sensitive */
// else if(argv[2]==DECRYPT)
else if(*argv[2]==DECRYPT)
{
(engine1->fh)=fopen(argv[1],"r"); // Opens file
if((engine1->fh)<=0)
{
printf("\n\nError opening file...");
(engine1->rv)=FILE_ERROR;
}
}
}

-Mike
 
E

Emmanuel Delahaye

zotkara a écrit :
Hello. I need help correcting the following C source code.
<...>
See my comments (-ed-). I have just made your code compilable. Other
errors may exist.

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

#define TITLE "coder\nAbout: encrypts or decrypts file."
#define USAGE "Usage: coder [filename]
[action]\n\t[action]\n\td\tDecrypt file\n\tc\tCrypt file"
#define FILE_ERROR -2
#define DECRYPT 'd'

/* -ed- missing 'struct'. It's an object : 'typedef' removed */
struct crypt
{
int rv; // Recovery value
int ch; // Channel
unsigned int ctr; // Counter
int val; // Encrypt value
char buffer[257]; // Buffer
FILE *fh; // File handle
}
engine0 =
{
1, 0, 0, 5
};

static void start(int argc, char argv[])
{
/* -ed- missing 'struct' */
struct crypt *engine1 = &engine0; // Turn on engine
if (argc != 3)
{
printf("%s\n%s\n", TITLE, USAGE);
}

/* Note that content of if(content) is case-sensitive */
else if (argv[2] == DECRYPT)
{
(engine1->fh) = fopen(argv[1], "r"); // Opens file
if ((engine1->fh) <= 0)
{
printf("\n\nError opening file...");
(engine1->rv) = FILE_ERROR;
}
}
}

int main(int argc, char *argv[])
{
start(argc, argv); // Start engine

}
 
E

Emmanuel Delahaye

zotkara a écrit :
Hello. I need help correcting the following C source code.
<...>
See my comments (-ed-). I have just made your code compilable. Other
errors may exist.

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

#define TITLE "coder\nAbout: encrypts or decrypts file."
#define USAGE "Usage: coder [filename]
[action]\n\t[action]\n\td\tDecrypt file\n\tc\tCrypt file"
#define FILE_ERROR -2
#define DECRYPT 'd'

/* -ed- missing 'struct'. It's an object : 'typedef' removed */
struct crypt
{
int rv; // Recovery value
int ch; // Channel
unsigned int ctr; // Counter
int val; // Encrypt value
char buffer[257]; // Buffer
FILE *fh; // File handle
}
engine0 =
{
1, 0, 0, 5
};

/* -ed- missing '*' before argv... */
static void start(int argc, char *argv[])
{
/* -ed- missing 'struct' */
struct crypt *engine1 = &engine0; // Turn on engine
if (argc != 3)
{
printf("%s\n%s\n", TITLE, USAGE);
}

/* Note that content of if(content) is case-sensitive */

/* -ed- missing [0] */
else if (argv[2][0] == DECRYPT)
{
(engine1->fh) = fopen(argv[1], "r"); // Opens file
if ((engine1->fh) <= 0)
{
printf("\n\nError opening file...");
(engine1->rv) = FILE_ERROR;
}
}
}

int main(int argc, char *argv[])
{
start(argc, argv); // Start engine

}
 
Z

zotkara

..


zotkara said:
Hello. I need help correcting the following C source code.

/* coder.c */
/* Usage: coder [filename] [action]
[action]
D decrypt
C crypt
*/

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

#define TITLE "coder\nAbout: encrypts or decrypts file."
#define USAGE "Usage: coder [filename] [action]\n\t[action]\n\td\tDecrypt
file\n\tc\tCrypt file"
#define FILE_ERROR -2
#define DECRYPT 'd'

void start(int argc,char argv[]);
int encode_character(int ch,int val);
int decode_character(int ch,int val);

typedef crypt

typedef struct crypt
{
int rv; // Recovery value
int ch; // Channel
unsigned int ctr; // Counter
int val; // Encrypt value
char buffer[257]; // Buffer
FILE *fh; // File handle
}engine0={1,0,0,5};

int main(int argc,char *argv[])
{
start(argc,argv); // Start engine

}

void start(int argc,char argv[])
{
crypt *engine1=&engine0; // Turn on engine
if(argc!=3)
{
printf("%s\n%s\n",TITLE,USAGE);
}

/* Note that content of if(content) is case-sensitive */
else if(argv[2]==DECRYPT)
{
(engine1->fh)=fopen(argv[1],"r"); // Opens file
if((engine1->fh)<=0)
{
printf("\n\nError opening file...");
(engine1->rv)=FILE_ERROR;
}
}
}
EOF

The following is the error message output by GNU gcc compiler in Slackware
10.2 in bash.

error: syntax error before '{' token
coder.c:29: error: parse error before '}' token
coder.c:29: error: parse error before ',' token
coder.c:29: warning: excess elements in scalar initializer
coder.c:29: warning: (near initialization for `engine0')
docs@pyenos:~/work_and_play/c++_learn$ gcc coder.c -o coder
coder.c:22: error: syntax error before '{' token
coder.c:29: error: parse error before '}' token
coder.c:29: warning: excess elements in scalar initializer
coder.c:29: warning: (near initialization for `engine0')
coder.c:29: warning: excess elements in scalar initializer
coder.c:29: warning: (near initialization for `engine0')
coder.c:29: warning: excess elements in scalar initializer
coder.c:29: warning: (near initialization for `engine0')
coder.c:29: warning: data definition has no type or storage class
coder.c: In function `main':
coder.c:33: warning: passing arg 2 of `start' from incompatible pointer
type
coder.c: In function `start':
coder.c:39: error: `crypt' undeclared (first use in this function)
coder.c:39: error: (Each undeclared identifier is reported only once
coder.c:39: error: for each function it appears in.)
coder.c:39: error: `engine1' undeclared (first use in this function)
coder.c:48: warning: passing arg 1 of `fopen' makes pointer from integer
EOF

Please help me correcting this code as I am a newbie. Thanks in advance.

OK now it compiles. Whether it does what you want I have no idea.

/* coder.c */
/* Usage: coder [filename] [action]
[action]
D decrypt
C crypt
*/

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

#define TITLE "coder\nAbout: encrypts or decrypts file."
#define USAGE "Usage: coder [filename] [action]\n\t[action]\n\td\tDecrypt
file\n\tc\tCrypt file"
#define FILE_ERROR -2
#define DECRYPT 'd'

//void start(int argc,char argv[]);
void start(int argc,char *argv[]);
int encode_character(int ch,int val);
int decode_character(int ch,int val);

typedef struct
{
int rv; // Recovery value
int ch; // Channel
unsigned int ctr; // Counter
int val; // Encrypt value
char buffer[257]; // Buffer
FILE *fh; // File handle
} crypt ;

crypt engine0={1,0,0,5};

int main(int argc,char *argv[])
{
start(argc,argv); // Start engine

}

//void start(int argc,char argv[])
void start(int argc,char *argv[])

{
crypt *engine1=&engine0; // Turn on engine
if(argc!=3)
{
printf("%s\n%s\n",TITLE,USAGE);
}

/* Note that content of if(content) is case-sensitive */
// else if(argv[2]==DECRYPT)
else if(*argv[2]==DECRYPT)
{
(engine1->fh)=fopen(argv[1],"r"); // Opens file
if((engine1->fh)<=0)
{
printf("\n\nError opening file...");
(engine1->rv)=FILE_ERROR;
}
}
}

-Mike
 
Z

zotkara

..
zotkara a écrit :
Hello. I need help correcting the following C source code.
<...>
See my comments (-ed-). I have just made your code compilable. Other
errors may exist.

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

#define TITLE "coder\nAbout: encrypts or decrypts file."
#define USAGE "Usage: coder [filename]
[action]\n\t[action]\n\td\tDecrypt file\n\tc\tCrypt file"
#define FILE_ERROR -2
#define DECRYPT 'd'

/* -ed- missing 'struct'. It's an object : 'typedef' removed */
struct crypt
{
int rv; // Recovery value
int ch; // Channel
unsigned int ctr; // Counter
int val; // Encrypt value
char buffer[257]; // Buffer
FILE *fh; // File handle
}
engine0 =
{
1, 0, 0, 5
};

/* -ed- missing '*' before argv... */
static void start(int argc, char *argv[])
{
/* -ed- missing 'struct' */
struct crypt *engine1 = &engine0; // Turn on engine
if (argc != 3)
{
printf("%s\n%s\n", TITLE, USAGE);
}

/* Note that content of if(content) is case-sensitive */

/* -ed- missing [0] */
else if (argv[2][0] == DECRYPT)
{
(engine1->fh) = fopen(argv[1], "r"); // Opens file
if ((engine1->fh) <= 0)
{
printf("\n\nError opening file...");
(engine1->rv) = FILE_ERROR;
}
}
}

int main(int argc, char *argv[])
{
start(argc, argv); // Start engine

}
 
Z

zotkara

/* -ed- missing [0] */
else if (argv[2][0] == DECRYPT)
{

Why do I need to put argv[2][0] when argv[2][0]==argv[2];? Thank you.
 
R

Richard Heathfield

zotkara said:
/* -ed- missing [0] */
else if (argv[2][0] == DECRYPT)
{

Why do I need to put argv[2][0] when argv[2][0]==argv[2];? Thank you.

Assuming this is the second argument to main:

argv[2] has type char *.
argv[2][0] has type char.
Therefore, argv[2][0] cannot, as you claim, have the same value as argv[2].
 
Z

zotkara

/* -ed- missing [0] */
else if (argv[2][0] == DECRYPT)
{

Why do I need to put argv[2][0] when argv[2][0]==argv[2];? Thank you.

Sorry for multi-posting. Maybe because arv[2][(0+int line_entered)] when
line_entered from screen after the first scan exceeds one then it becomes
an issue, where argv[2][(0+n)]!=argv[2]. I think in this particular code
in the context of the program code it produces no direct errors but it is
not a correct way of coding, so I guess your point is valid.
 
Z

zotkara

..
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hello. I need help correcting the following C source code.

/* coder.c */
/* Usage: coder [filename] [action]
[action]
D decrypt
C crypt
*/

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

#define TITLE "coder\nAbout: encrypts or decrypts file."
#define USAGE "Usage: coder [filename] [action]\n\t[action]\n\td\tDecrypt file\n\tc\tCrypt file"
#define FILE_ERROR -2
#define DECRYPT 'd'

void start(int argc,char argv[]);
int encode_character(int ch,int val);
int decode_character(int ch,int val);

typedef crypt

what sort of type is crypt? Yes, you typedef'ed it, but what did you
typedef it to?
{
int rv; // Recovery value
int ch; // Channel
unsigned int ctr; // Counter
int val; // Encrypt value
char buffer[257]; // Buffer
FILE *fh; // File handle
}engine0={1,0,0,5};

[snip]

- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFDut5WagVFX4UWr64RArzUAJ91EcAdn+QYa3J4Z5sDGDjjMZdTWQCgv2+B
aMnGBLwDqYvcvgvsypv5v4s=
=Cmpy
-----END PGP SIGNATURE-----
 
Z

zotkara

i think *argv[] cannot be an array since it is a pointer. so i agree
with you if you'd said that argv[] is char* type. since both argv[2]
and arv[2][0] point to the same address of an array, and was subjected to
the same precondition for pointer initialization, i thought both argv[2]
and argv[2][0] would be of char type. your thoughts RCH?

zotkara said:
/* -ed- missing [0] */
else if (argv[2][0] == DECRYPT)
{

Why do I need to put argv[2][0] when argv[2][0]==argv[2];? Thank you.

Assuming this is the second argument to main:

argv[2] has type char *.
argv[2][0] has type char.
Therefore, argv[2][0] cannot, as you claim, have the same value as argv[2].
 
Z

zotkara

/* Fixed by MKW */
/* coder.c */
/* Usage: coder [filename] [action]
[action]
D decrypt
C crypt
*/

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

#define TITLE "coder\nAbout: encrypts or decrypts file."
#define USAGE "Usage: coder [filename] [action]\n\t[action]\n\td\tDecrypt
file\n\tc\tCrypt file"
#define FILE_ERROR -2
#define DECRYPT 'd'

//void start(int argc,char argv[]);
void start(int argc,char *argv[]); // Good point Mike! ;)
int encode_character(int ch,int val);
int decode_character(int ch,int val);

typedef struct
{
int rv; // Recovery value
int ch; // Channel
unsigned int ctr; // Counter
int val; // Encrypt value
char buffer[257]; // Buffer
FILE *fh; // File handle
} crypt ;

crypt engine0={1,0,0,5};

int main(int argc,char *argv[])
{
start(argc,argv); // Start engine

}

//void start(int argc,char argv[])
void start(int argc,char *argv[])

{
crypt *engine1=&engine0; // Turn on engine
if(argc!=3)
{
printf("%s\n%s\n",TITLE,USAGE);
}

/* Note that content of if(content) is case-sensitive */
// else if(argv[2]==DECRYPT)
else if(*argv[2]==DECRYPT)
{
(engine1->fh)=fopen(argv[1],"r"); // Opens file
if((engine1->fh)<=0)
{
printf("\n\nError opening file...");
(engine1->rv)=FILE_ERROR;
}
}
}

/* Fixed by EMD */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TITLE "coder\nAbout: encrypts or decrypts file."
#define USAGE "Usage: coder [filename]
[action]\n\t[action]\n\td\tDecrypt file\n\tc\tCrypt file"
#define FILE_ERROR -2
#define DECRYPT 'd'

/* -ed- missing 'struct'. It's an object : 'typedef' removed */
struct crypt
{
int rv; // Recovery value
int ch; // Channel
unsigned int ctr; // Counter
int val; // Encrypt value
char buffer[257]; // Buffer
FILE *fh; // File handle
}
engine0 =
{
1, 0, 0, 5
};

static void start(int argc, char argv[])
{
/* -ed- missing 'struct' */
struct crypt *engine1 = &engine0; // Turn on engine
if (argc != 3)
{
printf("%s\n%s\n", TITLE, USAGE);
}

/* Note that content of if(content) is case-sensitive */
else if (argv[2] == DECRYPT)
{
(engine1->fh) = fopen(argv[1], "r"); // Opens file
if ((engine1->fh) <= 0)
{
printf("\n\nError opening file...");
(engine1->rv) = FILE_ERROR;
}
}
}

int main(int argc, char *argv[])
{
start(argc, argv); // Start engine

}

/* Fixed by EMD */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TITLE "coder\nAbout: encrypts or decrypts file."
#define USAGE "Usage: coder [filename]
[action]\n\t[action]\n\td\tDecrypt file\n\tc\tCrypt file"
#define FILE_ERROR -2
#define DECRYPT 'd'

/* -ed- missing 'struct'. It's an object : 'typedef' removed */
struct crypt
{
int rv; // Recovery value
int ch; // Channel
unsigned int ctr; // Counter
int val; // Encrypt value
char buffer[257]; // Buffer
FILE *fh; // File handle
}
engine0 =
{
1, 0, 0, 5
};

/* -ed- missing '*' before argv... */
static void start(int argc, char *argv[])
{
/* -ed- missing 'struct' */
struct crypt *engine1 = &engine0; // Turn on engine
if (argc != 3)
{
printf("%s\n%s\n", TITLE, USAGE);
}

/* Note that content of if(content) is case-sensitive */

/* -ed- missing [0] */
else if (argv[2][0] == DECRYPT)
{
(engine1->fh) = fopen(argv[1], "r"); // Opens file
if ((engine1->fh) <= 0)
{
printf("\n\nError opening file...");
(engine1->rv) = FILE_ERROR;
}
}
}

int main(int argc, char *argv[])
{
start(argc, argv); // Start engine

}
 
R

Richard Heathfield

zotkara said:
i think *argv[] cannot be an array since it is a pointer.

argv is a pointer to the first element in an array of pointers to char.

argv has type char **.
argv[2] has type char *.
argv[2][0] has type char.
since both argv[2]
and arv[2][0] point to the same address of an array,

They don't. argv[2][0] is a char, not a pointer, so it doesn't point to the
same address of an array, it doesn't point to anything, and in fact it
doesn't even point.
 
E

Emmanuel Delahaye

zotkara a écrit :
i think *argv[] cannot be an array since it is a pointer.

It is an pointer that holds the address of an array of pointers to char.

For example :

The following command line

'$ myprog param1 param2'

is tokenized by the system in something similar to:

int argc = 3;
char *argv[] =
{
"myprog",
"param1",
"param2",
NULL
};

The type of argv is char **
The type of argv[x] is char *
The type of argv[x][y] is char

got it ?
so i agree
with you if you'd said that argv[] is char* type.

'argv[]' alone has no meaning. In the context of a function parameter,
'char argv[]' means 'char *argv'.
since both argv[2]
and argv[2][0] point to the same address of an array,

Wrong. argv[2][0] points nowhere. It's not a pointer but rather a char.
 
Z

zotkara

zotkara a écrit :
i think *argv[] cannot be an array since it is a pointer.

It is an pointer that holds the address of an array of pointers to char.

For example :

The following command line

'$ myprog param1 param2'

is tokenized by the system in something similar to:

int argc = 3;
char *argv[] =
{
"myprog",
"param1",
"param2",
NULL
};

The type of argv is char **
The type of argv[x] is char *
The type of argv[x][y] is char

got it ?
so i agree
with you if you'd said that argv[] is char* type.

'argv[]' alone has no meaning. In the context of a function parameter,
'char argv[]' means 'char *argv'.
since both argv[2]
and argv[2][0] point to the same address of an array,

Wrong. argv[2][0] points nowhere. It's not a pointer but rather a char.
 
Z

zotkara

zotkara said:
i think *argv[] cannot be an array since it is a pointer.

argv is a pointer to the first element in an array of pointers to char.

argv has type char **.
argv[2] has type char *.
argv[2][0] has type char.
since both argv[2]
and arv[2][0] point to the same address of an array,

They don't. argv[2][0] is a char, not a pointer, so it doesn't point to the
same address of an array, it doesn't point to anything, and in fact it
doesn't even point.
 
K

Keith Thompson

zotkara said:
i think *argv[] cannot be an array since it is a pointer. so i agree
with you if you'd said that argv[] is char* type. since both argv[2]
and arv[2][0] point to the same address of an array, and was subjected to
the same precondition for pointer initialization, i thought both argv[2]
and argv[2][0] would be of char type. your thoughts RCH?

The C FAQ is at <http://www.c-faq.com/>. Section 6, Arrays and
Pointers, is particularly relevant here, but you should consider
reading the whole thing.

And please don't top-post. Your response goes below, or interspersed
with, any quoted text -- and it's seldom necessary to quote the entire
article to which you're replying.
 
K

Keith Thompson

[nothing but quoted text]

Please don't change the Subject: header when you post a followup; it
makes it difficult to follow the thread.
 
R

Randy Howard

zotkara wrote
(in article said:
i think *argv[] cannot be an array since it is a pointer.

It's not wise to argue with Richard about C. Almost all of the
time he is correct. If you are having trouble with your code,
odds are you are nowhere near as experienced. Listen to him, he
has a great deal to offer for those that want to learn and have
an open mind.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Problem with enums, const char* and structs 9
pointer array problem? 7
struct problems 71
Initialization Problem 4
dice generator problems 43
Code Review Request: Newbie C programmer 15
Program Help 2
newbie question 6

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top