C++ 11代码:
int a[3];
auto b = a; // b is of type int*
auto c = &a; // c is of type int(*)[1]
Run Code Online (Sandbox Code Playgroud)
C代码:
int a[3];
int *b = a;
int (*c)[3] = &a;
Run Code Online (Sandbox Code Playgroud)
的价值观b和c是相同的.
b和之间有什么区别c?为什么他们不是同一类型?
更新:我将数组大小从1更改为3.
我正在从KandR C编程书中做一些有趣的练习.该程序用于从用户输入的一组行中查找最长行,然后打印它.
这是我写的(部分,部分内容直接取自本书): -
#include <stdio.h>
#include <stdlib.h>
int MAXLINE = 10;
int INCREMENT = 10;
void copy(char longest[], char line[]){
int i=0;
while((longest[i] = line[i]) != '\0'){
++i;
}
}
int _getline(char s[]){
int i,c;
for(i=0; ((c=getchar())!=EOF && c!='\n'); i++){
if(i == MAXLINE - 1){
s = (char*)realloc(s,MAXLINE + INCREMENT);
if(s == NULL){
printf("%s","Unable to allocate memory");
// goto ADDNULL;
exit(1);
}
MAXLINE = MAXLINE + INCREMENT;
}
s[i] = c;
}
if(c == '\n'){
s[i] = c;
++i; …Run Code Online (Sandbox Code Playgroud) 我这些天自己学习C++并且我有一些问题需要理解为什么这段代码不能编译使用#g++ -std=c++11 source.cpp.实际上我使用哪个特定标准并不重要,它只是不编译.
#include <iostream>
#include <string>
using namespace std;
int print_a(char array[])
{
for(char c : array)
cout << c;
cout << endl;
return 0;
}
int main(void)
{
char hello[] {"Hello!"};
print_a(hello);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误消息:
debian@debian:~/Documents$ g++ -std=c++11 source.cpp
source.cpp: In function ‘int print_a(char*)’:
source.cpp:6:15: error: ‘begin’ was not declared in this scope
for(char c : array)
^
source.cpp:6:15: note: suggested alternatives:
In file included from /usr/include/c++/4.9/bits/basic_string.h:42:0,
from /usr/include/c++/4.9/string:52,
from /usr/include/c++/4.9/bits/locale_classes.h:40,
from /usr/include/c++/4.9/bits/ios_base.h:41,
from /usr/include/c++/4.9/ios:42, …Run Code Online (Sandbox Code Playgroud) 我想知道是否有一种方法可以char*指向char数组的内容,以便可以修改char*跨函数。
例如
void toup(char* c) {
char array[sizeof(c)];
for (int x;x<strlen(c);x++){
array[x]=toupper(c[x]);
}
}
int main(){
char *c="Hello";
toup(c);
}
Run Code Online (Sandbox Code Playgroud)
试图使之array = char*似乎不起作用。是否可以使char *指向char数组?
在我本科 C 编程课程的大部分时间里,我们学习了 C99,我们的讲师从不费心教我们 C99 和以前版本之间的主要区别。
我们最近被告知,我们可能会被要求在下一次考试期间使用 C89 实施解决方案。
我的问题是关于函数内部声明和使用的可变长度多维数组的使用。
在 C99 中,我可以有这样的功能:
void func(int cols, int mat[][cols], int rows);
Run Code Online (Sandbox Code Playgroud)
而在 C89 中,VLA 和类似的装置是不合法的。有人告诉我,您需要改用指向指针的指针。所以像:
void func(int **mat, int cols, int rows);
Run Code Online (Sandbox Code Playgroud)
但是,我在理解方面遇到了问题:
mat[i][j]吗?int **mat;吗?我认为您必须使用malloc(),但我很难弄清楚确切的声明语句。边问。仍然关于可变大小的矩阵,有人告诉我有这样的设置:
int rows, cols;
// get rows and cols from stdinput
int mat[rows][cols];
Run Code Online (Sandbox Code Playgroud)
由于程序堆栈上的分配,这不是创建具有给定维度的矩阵的最佳方法。什么是更好的方法?
谢谢!
如何将C char**转换为C++向量?是否有一些内置功能可以用来做到这一点,或者通过一系列迭代步骤完成它是否更好?
编辑:由于各种原因,C数组中的元素数量未知.我有可能将其作为另一个参数传递,但这是绝对必要的吗?
通常,当您声明指针(例如int)时,您必须为其分配一个内存地址:
int value = 123;
int* p = &value;
Run Code Online (Sandbox Code Playgroud)
创建char指针时,可以为其指定char数组,而无需包含地址:
char* c = "Char Array";
Run Code Online (Sandbox Code Playgroud)
这是如何运作的?它是否分配内存并指向它?为什么其他类型的指针不能做同样的事情?
我刚从朋友处得到一个问题.
#include<stdio.h>
void fun(int[][3]);
int main(void){
int a[3][3]={1,2,3,4,5,6,7,8,9};
fun(a);
printf("\n%u",a);
a++;//Ques 1
printf("\n%u",a);
printf("%d",a[2][1]-a[1][2]);
return 0;
}
void fun(int a[][3]){
++a;//Ques 2
a[1][1]++;
}
Run Code Online (Sandbox Code Playgroud)
第1行将抛出L值的错误,因为'a'是二维数组的名称.但是,对于第2行的情况,情况并没有发生.
谁能明白这个疑问?
以下是使用C++实现哈希表.你能帮我理解一下HashEntry **table是什么吗?为什么它被声明为双指针?它是一个数组,数组的每个值是HashEntry?
class HashEntry {
private:
int key;
int value;
public:
HashEntry(int key, int value) {
this->key = key;
this->value = value;
}
int getKey() {
return key;
}
int getValue() {
return value;
}
};
const int TABLE_SIZE = 128;
class HashMap {
private:
HashEntry **table;
public:
HashMap() {
table = new HashEntry*[TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++)
table[i] = NULL;
}
int get(int key) {
int hash = (key % …Run Code Online (Sandbox Code Playgroud) 我正在查看一些我没写过的代码,希望能帮助理解它的一个元素.代码存储字符数组,创建指向这些数组的指针(指定数组地址的指针).它看起来像然后创建一个数组来存储这些字符指针地址,我只想澄清我正在看的东西.我也很困惑在创建数组时使用double(**).
我已经在下面列出了我正在查看的简化示例.
char eLangAr[20] = "English";
char fLangAr[20] = "French";
char gLangAr[20] = "German";
char* eLangPtr = eLangAr;
char* fLangPtr = fLangAr;
char* gLangPtr = gLangAr;
char **langStrings [3]=
{
&eLangPtr,
&fLangPtr,
&gLangPtr
};
Run Code Online (Sandbox Code Playgroud)
使用数组时,它们将它作为参数传递给函数.
menu (*langStrings[0]);
Run Code Online (Sandbox Code Playgroud)
所以我的想法是将字符数组值"English"传递给函数,但是我很难看到它.它们将菜单函数传递给存储在位置0的langStrings函数中的值的副本,这将是eLandPtr的地址?如果有人能用英语解释这个过程,那么我可以把它放在一边,这将是很棒的.这可能只是因为它已经过了漫长的一天,但它根本就不在我脑海里.