我知道数组名称可以用作指针(尽管它是转换后的形式),但我的问题是,是否有一些其他实例,其中数组充当指针.
我试图使用"g ++ main.cpp -c"编译下面的代码,但它给了我这个奇怪的错误..任何想法?
main.cpp: In function ‘int main()’:
main.cpp:9:17: error: invalid conversion from ‘Graph*’ to ‘int’
main.cpp:9:17: error: initializing argument 1 of ‘Graph::Graph(int)’
main.cpp:10:16: warning: deprecated conversion from string constant to ‘char*’
Run Code Online (Sandbox Code Playgroud)
这是我正在尝试编译的主要模块,下面是我在graph.hpp中的图形类
#include <iostream>
#include "graph.hpp"
using namespace std;
int main()
{
Graph g;
g = new Graph();
char* path = "graph.csv";
g.createGraph(path);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我的Graph类
/*
* graph.hpp
*
* Created on: Jan 28, 2012
* Author: ajinkya
*/
#ifndef _GRAPH_HPP_
#define _GRAPH_HPP_
#include "street.hpp"
#include …Run Code Online (Sandbox Code Playgroud) 可能重复:
C中的char s []和char*s有什么区别?
char a [] ="string"之间的区别; char*p ="string";
首先,我想问一下我在哪里可以学习char*和char []的所有基础知识.
大多数时候,我发现自己在如何比较和如何申报方面苦苦挣扎.
例1:
char *test = "hello world";
Run Code Online (Sandbox Code Playgroud)
这将在编译时产生以下警告:
warning: deprecated conversion from string constant to ‘char*’
Run Code Online (Sandbox Code Playgroud)
例2:
vector<char*> test2;
test2.push_back("hello world");
Run Code Online (Sandbox Code Playgroud)
这将产生复制字符串的错误.
所以我想出的解决方案是:
(它是否正确?)
vector<char*> test3;
char *str1 = "hello world"
test3.push_back(str1);
Run Code Online (Sandbox Code Playgroud)
提前致谢!:)
============================================
这里的人提供了两个好的读物:
可能重复:
数组名是C中的指针吗?
#include <stdlib.h>
int main(int argc, const char *argv[])
{
char *b=(char*)malloc(sizeof(char)*50);
b=(char*)"hello world";
// works
char a[50];
a=(char*)"hello world";
//doesn't work. why? I thought array names are just pointers that point
//to the first element of the array (which is char). so isn't a char*?
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我认为它不起作用的原因是因为没有名为"a"的变量实际上存储了char*值.那么'a'应该被视为右值吗?我不确定我是否正确理解了这个概念
我在C++中使用指针时遇到了一些问题.我用不同大小的数组实现了三种方法,但计算方法相同.所以我决定提取计算并将它们放在一个需要数组的新方法中.但这不起作用,我不知道如何修改我的程序.
void method1() {
float a[3][3];
calculate(a, 3);
}
void method2() {
float a[4][4];
calculate(a, 4);
}
void method3() {
float a[5][5];
calculate(a, 5);
}
void calculate(float *param[], int n) {
// Code
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用全局变量或向量等但我需要这个结构中的逻辑.
这是编译器错误:
Test.cpp: In function 'void method1()':
Test.cpp:7:16: error: cannot convert 'float (*)[3]' to 'float**' for argument '1' to 'void calculate(float**, int)'
Test.cpp: In function 'void method2()':
Test.cpp:12:16: error: cannot convert 'float (*)[4]' to 'float**' for argument '1' to 'void calculate(float**, int)'
Test.cpp: In function 'void …Run Code Online (Sandbox Code Playgroud) 我已经研究过并且我清楚地知道数组不是指针
但是我已经学到了一些我认为可能使用指针声明声明类似数组的变量的东西.我知道的事情:
int *x;a[b]➜*(a+b)a[b][c]➜*(*(a+b)+c)a[3],它在堆栈上分配3个空的内存地址.*(a+0),*(a+1),*(a+3)是在堆栈的使用分配.
Ideone Code
#include <stdio.h>
char block[3]={1,2,3};// This line will be removed
//And would create a pointer declaration of that having array-like structure
int main() {
while(i < sizeof(block))
printf("%d", block[i++]);
//And that I still …Run Code Online (Sandbox Code Playgroud) 如果我声明两个数组,一个在堆栈上,一个在堆上,我在打印变量名时会得到不同的行为(如果重要的话,在gdb中).
int array_on_stack[5];
int * array_on_heap = new int[5];
Run Code Online (Sandbox Code Playgroud)
现在在gdb中,我逐步执行每行代码,然后打印变量名,期望获取每个代码的内存地址.
print array_on_stack
print array_on_heap
Run Code Online (Sandbox Code Playgroud)
但是array_on_stack它打印数组的内容而不是内存地址.为了获取内存地址,我需要命令print &array_on_stack.这表明这array_on_stack不是一个指针.有人可以解释这两个声明在如何访问其内存地址方面的区别以及为什么会出现这种情况?
可能重复:
数组名是C中的指针吗?
所以,我通常使用指针声明数组.
但是,您也可以使用方括号表示法声明数组:
char a[] = "ok" ;
char b[] = "to" ;
char *pa = a ;
cout << "a " << sizeof( a ) << endl ; // 3
cout << "pa " << sizeof( pa ) << endl ; // 4
Run Code Online (Sandbox Code Playgroud)
奇怪的是,sizeof( a )将是以字节为单位的数组的实际大小,而不是指针的大小.
我发现这很奇怪,因为指针在哪里呢?方括号声明的数组实际上是一种带(sizeof(char)*numElements)字节的数据结构吗?
此外,您无法将a重新分配给b:
a = b ; // ILLEGAL.
Run Code Online (Sandbox Code Playgroud)
这是为什么?似乎a 是数组而不是指向数组的指针("左操作数必须是l值"是a = b上面的错误).是对的吗?
可能重复:
数组名是C中的指针吗?
假设我有一个char数组说arr和arr将代表第一个元素的地址,所以arr ++应该是完全合法的,那么为什么编译器会说'左值'.
如果我这样做:arr = arr + 1那么为什么它是无效的转换.我只是一个一个地增加指针.编译器告诉LHS操作数类型是char [4],但在RHS上它是char*.
main()
{
char arr[]={'a','b','c','d'};
for(;arr!=arr+4;arr++) //lvalue required
printf("%c",*arr);
}
Run Code Online (Sandbox Code Playgroud) 对于以下代码,我想知道是否只在堆栈中创建了一个数组,或者是否还有一个在静态中创建的数组.我只是对字符串中的数组创建感到困惑.
char str[] = "White";
Run Code Online (Sandbox Code Playgroud)
我假设这会在堆栈中创建一个名为str的指针,该指针指向具有以下内容的数组"White\0",在静态内存中.这是正确的假设吗?