jfbode1029
Posts
-
Tell me about the c programming languages -
How cin.get() works in loop ?cin.get()
does not read individual keystrokes -- the input is buffered by the terminal until you hit Enter, then all of the input is sent to the program at once. To read individual keystrokes, you'll have to rely on a third-party tool like GNU's ncurses library. -
Why does most C/C++ developer prefers char *c instead of char* c?There are indeed pointer types, but there are no pointer type specifiers. The pointer-ness of something is specified by a combination of the type specifier(s) and the declarator. For a pointer like
char *p;
the type of
p
is "pointer tochar
" orchar *
, but that type is obtained by the combination of the type specifierchar
and the pointer declarator*p
. Sure, you can write it aschar* p;
, but it will be parsed aschar (*p);
. Something likeT *p1, *p2, *p3;
should be perfectly understandable to anyone who understands C and C++ declaration syntax, but nobody does because it's not taught properly for some bizarre reason. It's such a fundamental part of the language, but it's invariably glossed over in every introductory text so as not to scare off beginners. So people inevitably bluescreen the first time they see something like a pointer to an array or a pointer to a function because the simplistic-to-the-point-of-being-wrong version of C declaration syntax they were taught doesn't account for it. Yes, pointers are scary and difficult to understand at first. They're also a fundamental part of C programming (less so C++, but still important). C (and C++) declaration syntax needs to be taught properly and completely. But since it isn't, we need to come up with a bunch of coding standards to make up for it. So, no more than one declaration per line, because it's too hard to explain how
char* a, b;
really works. Once you understand the rules, pointer declarations (including pointers to arrays, pointers to functions, pointers to arrays of functions returning pointers to arrays of pointers to int) make perfect sense. But apparently nobody understands the rules, so the language looks arbitrary and capricious. -
Return a local 2d ArrayThe return type for
getArray
needs to beint (*getArray())[COL];
. An expression of typeT [M][N]
decays to typeT (*)[N]
, notT **
. So your code needs to beint main()
{
int (*ptr)[N];
int (*getArray())[N];
...
}int (*getArray())[N]
{
...
return tab;
} -
i would like to be a programmerProgramming is more than just writing code - it's about solving problems with code. You'll need to develop analysis and troubleshooting skills in addition to straight coding skills. Having said that, the best way to learn how to write code is to actually write code. You could start by looking at Codecademy. It's free, it's all done in the browser (so you don't have to set up a development environment on your local machine), and they cover a useful range of languages. I haven't used it much myself, so I can't comment on the quality of the instruction. It may be good, it may be awful. Depends on what you want to learn.
-
Why does most C/C++ developer prefers char *c instead of char* c?Because that's how both the C and C++ grammars work. The asterisk binds to the thing being declared, not the type. A declaration like
int* a, b;
is parsed as
int (*a), b;
Declarations in C (and simple declarations in C++) are a sequence of declaration specifiers followed by one or more (optionally initialized) declarators. The declarator is what specifies the name along with the the pointer-ness, array-ness, and/or function-ness of the thing being declared. Given a sequence of declaration specifiers
D
1, then the following are all true:D *p; // p is a pointer to D
D a[N]; // a is an array of D
D f(); // f is a function returning DD *ap[N]; // ap is an array of pointers to D
D *fp(); // fp is a function returning a pointer to DD (*pa)[N]; // pa is a pointer to an array of D
D (*pf)(); // pf is a pointer to a function returning DD (*fpa())[N]; // fpa is a function that returns a pointer to an array of D
D (*apf[N])(); // apf is an array of pointers to functions returning D// even more complex combinations are possible!!!
The things following
D
are the declarators.*p
is a declarator, as isa[N]
, as is*ap[N]
, as is(*pa)[N]
. Since both[]
and()
operators have higher precedence than unary*
, an expression like*a[N]
parses as*(a[N])
. To declare a pointer to an array or a pointer to a function, you must explicitly group the*
operator with the thing that points to the array or function. This is why the common convention (at least among C programmers) ischar *c;
and notchar* c;
. I understand where thetype* name
andtype& name
conventions came from in C++, and when I write C++ I follow that convention. But I feel dirty every time I do it.
1. Declaration specifiers include storage class specifiers like
static
,auto
, andtypedef
, type specifiers likeint
,double
,struct foo
, and type qualifiers likeconst
orvolatile