ok覆盖声明:char * a =" abc&#34 ;;?

时间:2017-07-14 18:33:33

标签: c

建议如果缓冲区声明如下:

char * a = "one";

它不应该被重写。

a = "two";
a = "three";
a = "ab";

为什么这会是一件坏事呢?

因为这似乎被大多数人所接受。

char * a = "";
a = "abc";

如果可以的话。为什么上面的例子不合适?

3 个答案:

答案 0 :(得分:1)

指定C:\Users\delacrga\Ionic projects\myApp>ionic cordova build android --prod --release -- -- --keystore=filename.keystore --alias=myalias Running app-scripts build: --prod --iscordovaserve --externalIpRequired --nobrowser [08:52:04] build prod started ... [08:52:04] clean started ... [08:52:04] clean finished in 10 ms [08:52:04] copy started ... [08:52:04] ngc started ... [08:52:15] ngc finished in 11.48 s [08:52:15] preprocess started ... [08:52:16] deeplinks started ... [08:52:16] deeplinks finished in 63 ms [08:52:16] optimization started ... [08:52:16] copy finished in 11.85 s [08:52:26] optimization finished in 10.47 s [08:52:26] preprocess finished in 10.54 s [08:52:26] webpack started ... [08:52:32] webpack finished in 5.46 s [08:52:32] sass started ... [08:52:32] uglifyjs started ... [08:52:33] sass finished in 1.22 s [08:52:33] cleancss started ... [08:52:35] cleancss finished in 1.93 s [08:52:42] uglifyjs finished in 10.23 s [08:52:42] postprocess started ... [08:52:42] postprocess finished in 15 ms [08:52:42] lint started ... [08:52:42] build prod finished in 37.83 s > cordova build android --release -- --keystore=filename.keystore --alias=myalias × Running command - failed! [ERROR] Cordova encountered an error. You may get more insight by running the Cordova command above directly. [ERROR] An error occurred while running cordova build android --release -- --keystore=filename.keystore --alias=myalias (exit code 1): ANDROID_HOME=C:\Users\delacrga\AppData\Local\Android\sdk JAVA_HOME=C:\Program Files\java\jdk1.8.0_131 Subproject Path: CordovaLib The Task.leftShift(Closure) method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use Task.doLast(Action) instead. at build_amlj4yw9chmdse17z8bfqqws2.run(C:\Users\delacrga\Ionic projects\myApp\platforms\android\build.gradle:139) FAILURE: Build failed with an exception. * Where: Build file 'C:\Users\delacrga\Ionic projects\myApp\platforms\android\build.gradle' line: 289 * What went wrong: A problem occurred evaluating root project 'android'. > Keystore file does not exist: C:\Users\delacrga\Ionic projects\myApp\platforms\android\..\..\filename.keystore * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. BUILD FAILED Total time: 4.175 secs Error: cmd: Command failed with exit code 1 Error output: FAILURE: Build failed with an exception. * Where: Build file 'C:\Users\delacrga\Ionic projects\myApp\platforms\android\build.gradle' line: 289 * What went wrong: A problem occurred evaluating root project 'android'. > Keystore file does not exist: C:\Users\delacrga\Ionic projects\myApp\platforms\android\..\..\filename.keystore * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. 指向不同的字符串文字不是问题;你写的很好。

尝试覆盖a 指向的字符串文字有什么不妥。 IOW,给出了这一行

a

以下任何一种情况都会导致未定义的行为,这意味着您的代码可能会崩溃,或者可能会有效,或者可能会损坏其他数据:

char * a = "abc";

等。

如果您知道a[0] = 'A'; strcpy( a, "foo" ); *a = *a + 1; 只会指向字符串文字,最好将其声明为a

const

您仍然可以指定const char * a = "abc"; 指向不同的字符串文字:

a

但是如果你试图修改a = "foo"; a = "bar"; 指向的内容,编译器会对你大喊大叫。

答案 1 :(得分:0)

用任何其他char *variable

重写"string"是完全可以的

但请记住,这样写的字符串是常量,因此存储在应用程序的二进制部分中,从而增加了它的大小。

此外,在应用程序中分散字符串会使其维护变得更加困难。您应该将所有字符串集中到一个<headerfile.h>文件中并包含该文件。

然后使用常量来引用字符串。

答案 2 :(得分:0)

char * a = "one";基本上等同于

char const * a = "one"; 

这意味着a是可修改的,但不是字符串a指向的。因此,根据代码的要求修改a并不是一种不好的做法。