Char declaration in C

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What is the difference between char *infile = argv[1]; and char infile = argv[1]; ?
What is the * used for?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It's a pointer. The second example does not make a lot of sense. You might do:

char infile = argv[1][0];

(for example) because you can apply one character from the argv[1] array to a char variable but you can't assign the entire argv[1] to a single character!

 

Note that argv is defined as one of:

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

or

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

So argv is an array of char pointers. argv[1] is just one element of this so it is a "char *" not just "char".

 

And if you don't understand what I am talking about you have missed possibly the most important reason in the world why everyone chooses to write in C! Time to go back to your C book and read about pointers, address-of, pass by reference and other key C concepts).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

vcoder wrote:

What is the * used for?

Must be to denote a better way of coding.

In different contexts an asterisk might have a different meaning.  But I'm surprised you don't have a C reference/primer of some type.

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you have not worked with pointers before, then start gently.

Not with pointers & array combinations but just simple pointer to simple variables.

Read a tutorial on pointers in general and experiment with some code.

(I haven't read anything from the link below, but cplusplus tends to have decent tutorials.

http://www.cplusplus.com/search.do?q=tutorial+pointer+

 

Then later add more complexity or more layers of indirection.

Pointers to functions are also a very nice concept and extremely usefull in some situations.

Doing magic with a USD 7 Logic Analyser: https://www.avrfreaks.net/comment/2421756#comment-2421756

Bunch of old projects with AVR's: http://www.hoevendesign.com