* ptrj ++执行后,ptrj值不应该为4吗?
int j=3,*ptrj = NULL;
ptrj = &j;
*ptrj++;
printf("%i",*ptrj);
Run Code Online (Sandbox Code Playgroud) 还行吧:
$foo++ if $condition;
Run Code Online (Sandbox Code Playgroud)
这没关系:
$foo++ for (1..10);
Run Code Online (Sandbox Code Playgroud)
但这不是:
$foo++ if $condition for (1..10);
Run Code Online (Sandbox Code Playgroud)
如果事情并不复杂,我发现后者非常易读,而且它适用于一行!有没有办法做到这一点,还是我应该继续我的生活?
我正在编写一个简单的字符串连接程序.
该程序按我发布的方式工作.但是,我首先使用以下代码编写它来查找字符串的结尾:
while (*s++)
;
Run Code Online (Sandbox Code Playgroud)
但是,该方法不起作用.传递给它的字符串未正确复制.具体来说,我试图将"abc"复制到一个持有"\ 0"的char []变量.
从阅读C K&R书籍看起来应该可行.紧凑的表格应采取以下步骤.
那为什么不起作用呢?我在Debian上用gcc编译.
我发现这个版本确实有效:
strncat(char *s, const char *t, int n)
{
char *s_start = s;
while (*s)
s++;
for ( ; n > 0 && *t; n--, s++, t++)
*s = *t;
*(s++) = '\0';
return s_start;
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
当使用运算符重载前缀和后缀增量时,我从编译器收到错误:
"Fajl Fajl :: operator ++(int)':已定义或声明的成员函数"
以下是operator ++的标题:
Fajl& operator ++ (); // prefix
Fajl& operator -- (); // prefix
Fajl operator ++ (int); // postfix
Fajl operator ++ (int); // postfix
Run Code Online (Sandbox Code Playgroud)
而我的实施:
Fajl& Fajl::operator ++ () // prefix
{
++(*poz);
return *this;
}
Fajl& Fajl::operator -- () // prefix
{
--(*poz);
return *this;
}
Fajl Fajl::operator ++ (int dummy) // postfix
{
Fajl temp(*this);
++(*this);
return temp;
}
Fajl Fajl::operator -- (int dummy) // postfix
{
Fajl temp(*this);
--(*this); …Run Code Online (Sandbox Code Playgroud) c++ operator-overloading prefix-operator postfix-operator operator-keyword
我已经提供了以下代码.当我重载一个重载的后缀运算符时,编译器抛出错误.它在重载的前缀运算符上工作正常.错误
error: no match for ‘operator<<’ in ‘std::cout << cDigit.Digit::operator++(0)’
Run Code Online (Sandbox Code Playgroud)
码
#include <iostream>
using namespace std;
class Digit
{
private:
int m_nDigit;
public:
Digit(int nDigit=0)
{
m_nDigit = nDigit;
}
Digit& operator++(); // prefix
Digit& operator--(); // prefix
Digit operator++(int); // postfix
Digit operator--(int); // postfix
friend ostream& operator<< (ostream &out, Digit &digit);
int GetDigit() const { return m_nDigit; }
};
Digit& Digit::operator++()
{
// If our number is already at 9, wrap around to 0
if (m_nDigit == 9) …Run Code Online (Sandbox Code Playgroud) 为什么下面的代码是非法的?
using System;
class Program
{
static void Main(string[] args) {
int i = 0;
--i++;
Console.WriteLine(i);
}
}
Run Code Online (Sandbox Code Playgroud)
它给出了以下错误--i++:
The operand of an increment or decrement operator must be a variable, property or indexer
我知道这段代码没有实际用途; 我只是好奇为什么不允许这样做.我不关心它可以通过删除该行而没有其他影响来修复.由于这是用语言律师标记的,请包括语言规范中的证据.
我有一种感觉,我会在这里感到非常愚蠢,但我只是在学习使用++和--增加和减少 while 循环的变量,并且想知道为什么这段代码有效,为什么这不起作用?
错误代码:
int ctr = 0;
while (ctr < 10)
printf("%d",ctr);
ctr=ctr+1;
Run Code Online (Sandbox Code Playgroud)
坏代码无限期地输出零。
工作代码:
int ctr=0;
while (ctr++ < 10)
printf("%d",ctr);
Run Code Online (Sandbox Code Playgroud)
想法是输出为 012345678910,但即使在工作代码中,它也从 1 开始并变为 10,而不是从 0 开始。即使 ctr 的初始值是 0。
在以下示例中:
typedef struct {
const char *description;
float value;
} swag;
typedef struct {
swag *swag;
const char *sequence;
} combination;
typedef struct {
combination numbers;
const char *make;
} safe;
int main()
{
swag gold = {"GOLD!", 1000000.0};
combination numbers = {&gold, "6502"};
safe s = {numbers, "RAMACON250"};
//Correct handling
printf("Result: %s \n", s.numbers.swag->description);
//Faulty handling
// printf("Result: %s \n", s.numbers.(*swag).description);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
以下行是正确的,以便接收 "GOLD!"
printf("Result: %s \n", s.numbers.swag->description);
Run Code Online (Sandbox Code Playgroud)
但为什么以下不正确,因为它(*x).y是相同的x->y
printf("Result: %s \n", s.numbers.(*swag).description);
Run Code Online (Sandbox Code Playgroud)
我在编译期间收到以下错误: …
我正在使用C运算符优先级表来更好地理解C的运算符优先级.我在理解以下代码的结果时遇到问题.
int a, b;
a = 1;
b = a++; // does not seem to follow C operator precedence
Run Code Online (Sandbox Code Playgroud)
使用C运算符的优先级表,我无法解释为什么使用postfix ++运算符,首先计算赋值,然后计算增量.
后缀增量运算符(++)在C中具有最高优先级,赋值运算符(=)具有最低优先级.所以在上面的代码中,第一个postfix ++必须执行然后赋值=.为此两个变量a和b应等于2,但他们没有.
为什么C运算符优先级似乎不适用于此代码?
postfix ++的最高优先级何时显示?
我刚刚遇到这个 C 问题,代码如下:
int fun();
int main()
{
for (fun(); fun(); fun())
printf("%d\n", fun());
return 0;
}
int fun()
{
int static n = 10;
return n--;
}
Run Code Online (Sandbox Code Playgroud)
我期望for循环计算为for(10;9;8)并printf打印 7 (因为我们将使用fun()?进行调用n = 7),但它打印了8。为什么会出现这种情况?