declaration

mixed declarations and codes

早过忘川 提交于 2019-12-01 03:28:50
When I compile function with "gcc -o dene -Wall -ansi -pedantic-errors dene.c" ,gcc emits no error.(can you look a line which starts with char ....,in if loop,) static void remove_negation(char *s,char *s1) { char **cmainp=malloc(sizeof(char*)*1); int len=0;int d=0; int i=0; cmainp[0]=malloc(sizeof(char)*300); len=strlen(s); for(i=0;i<len;++i) { if(s[i]=='-') if(i==0 || s[i-1]==',') /*look*/ {char *p=malloc(sizeof(char)*3); /*look*/ ++i; p[0]=s[i]; p[1]='\0'; strcat(s1,","); strcat(s1,p); free(p); continue; } cmainp[0][d]=s[i]; ++d; } cmainp[0][d+1]='\0'; strcpy(cmainp[0],s); free(cmainp[0]);

Why does this use of comma work in a expression but fail in a declaration?

那年仲夏 提交于 2019-12-01 03:05:58
问题 Am from high level OOP languages C# and Java and recently started scratching my head in C. I feel C a bit weird as equally as one feels JS is. So want to clarify below: Below gives error and that seems intuitive as it looks like incorrect syntax even in OOP languages int i=0,1,2; /* Error : expected identifier or ‘(’ before numeric constant int i = 0, 1, 2; ^ */ However below works surprisingly: int i; i = 0,1,2; //works Why is this behavior? Is their any significance to keep such behavior or

Why is the C++ variable declaration syntax inconsistent?

走远了吗. 提交于 2019-12-01 02:48:18
when I declare C++ variables, I do it like this: int a,b,c,d; or string strA,strB,strC,strD; I.e., first the type then a comma separated list of variable names. However, I declare pointers like this: int *a,*b,*c,*d; and references like this: int &a,&b,&c,&d; To be consistent it should be int* a,b,c,d; and int& a,b,c,d; Why is it not consistent? It's because of the C heritage. The * modifier applies to the variable in C. So the C++ designers made & to apply to the variable as well by analogy, since they couldn't change the first without breaking C compatibility. Same is true for the array

Why does c++ pointer * associate to the variable declared, not the type?

故事扮演 提交于 2019-12-01 02:46:34
Why was C++ designed such that the correct way to declare two int *s on the same line is int *x, *y; not int* x,y; I know some people think you should avoid either form and declare every variable on its own line, but I'm interested in why this language decision was made. To keep compatibility with C code, because that's how C works. Bjarne makes a good point here : The choice between int* p; and int *p; is not about right and wrong, but about style and emphasis. C emphasized expressions; declarations were often considered little more than a necessary evil. C++, on the other hand, has a heavy

Understanding two ways of declaring a C string [duplicate]

夙愿已清 提交于 2019-12-01 02:19:42
This question already has an answer here: How to declare strings in C [duplicate] 4 answers A few weeks ago I started learning the programming language C. I have knowledge in web technologies like HMTL/CSS, Javscript, PHP, and basic server administration, but C is confusing me. To my understanding, the C language does not have a data type for strings, just characters, however I may be wrong. I have heard there are two ways of declaring a string. What is the difference between these two lines of declaring a string: a.) char stringName[]; b.) char *stringName; I get that char stringName[]; is an

abstract class declarations in c++

雨燕双飞 提交于 2019-12-01 02:13:32
问题 Suppose foo is an abstract class in a C++ program, why is it acceptable to declare variables of type foo* , but not of type foo ? 回答1: Because if you declare a foo you must initialize/instantiate it. If you declare a *foo, you can use it to point to instances of classes that inherit from foo but are not abstract (and thus can be instantiated) 回答2: You can not instantiate an abstract class. And there are differences among following declarations. // declares only a pointer, but do not

Should a const static variable be initialized in a c++ header file?

ぃ、小莉子 提交于 2019-12-01 01:14:33
问题 my_test.h #ifndef MY_TEST #define MY_TEST struct obj { int x; int y; }; class A { private: const static int a=100; const static obj b; }; const obj A::b={1,2}; #endif When compiling cpp using this header file, an error 'multiple definition of 'A::b' occurs. why is this when I have been using guard macro already? why does A::a not produce the erro? (I can't write code const static obj b={1,2} in class A ) 回答1: why is this when I have been using guard macro already? Header guards only prevent

Understanding Const expression in VBScript

丶灬走出姿态 提交于 2019-12-01 01:12:35
问题 Well, I try to understand limitations in Const expressions in VBScript . I was not able to use anything except literals. What the docs say is: Literal or other constant, or any combination that includes all arithmetic or logical operators except Is . So, if "that includes all arithmetic or logical operators" then logically I expect I can do something like this: Const X = (1 + 2) But that brings the error "Expected literal constant". I found an interesting answer here that allows one to cheat,

Create an instance of a class in the class itself [duplicate]

不打扰是莪最后的温柔 提交于 2019-12-01 00:18:26
问题 This question already has answers here : Can a c++ class include itself as an member? (4 answers) Closed 5 years ago . I am new to C++ and have a question: Compare following code: class Node { public: int data; Node* x; }; and class Node { public: int data; Node x; }; I know the second part of code can't pass compile. But I want to know the reason. Is it related to memory allocation or just syntax regulation? I would be appreciate if someone can solve my question. 回答1: The code in your second

Variable declaration and definition

限于喜欢 提交于 2019-11-30 23:56:54
int x; Is this a declaration or a definition? As I write the following code, #include <stdio.h> int main(void) { int x; printf("%p",&x); return 0; } it prints some address. So as memory is allocated, int x; can't be just a declaration. So is it a definition? From the C standard (n1256) : 6.7 Declarations ... 5 A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that: — for an object, causes storage to be reserved for that object; — for a function, includes the function body; 101) — for an