我试图逐行读取文件.将每一行转换为以空字符结尾的字符串.将所有行推入向量并返回.
vector<const char*> TypeGLscene::LoadGLshader (string ThisFile)
{
ifstream fromFile;
fromFile.open(ThisFile);
if (fromFile.good()==false)
{
cout<<"Could not find file: "<<ThisFile<<endl;
exit(1);
}
vector<const char*> FileContents;
const char* OneLine;
string LineStr;
while (fromFile.good()==true)
{
getline(fromFile,LineStr);
OneLine = LineStr.c_str();
FileContents.push_back(OneLine);
}
fromFile.close();
return FileContents;
Run Code Online (Sandbox Code Playgroud)
问题是所有char字符串都是在堆栈中创建的,函数返回char*的向量.
我试图通过以下方式在堆上分配内存:
OneLine = new char[LineStr.size()+1];
Run Code Online (Sandbox Code Playgroud)
但我被卡住了,因为一旦分配,我就无法复制任何内容; 内容是const.
我怎么能在const char*上使用new关键字并在它实现它是const之前同时向它添加内容?
(更不用说我必须一个一个地删除它们,另一方面......真是一团糟)
编辑:我宁愿返回一个向量,但这一切都是因为我不知道快速(一行)方式将向量转换为const char**:
void glShaderSource(GLuint shader,
GLsizei count,
const GLchar **string,
const GLint *length);
Run Code Online (Sandbox Code Playgroud) 理解指针以及如何将它们与函数一起使用时遇到很多麻烦.尝试编写一个函数,它接受任意大小的字符数组并将其保存在struct.This是我尝试但得到seg错误.
#define LENGTH 50
typedef struct{
char *pass;
int length;
}pass_t;
void createNewPassword(pass_t *password);
int main(int argc, char *argv[]) {
pass_t *password=NULL;
createNewPassword(password);
printf("%d",password->length);
}
void
createNewPassword(pass_t *password){
char c;
int i, n=LENGTH;
if (!(password->pass=malloc((n+1)*sizeof(char)))) {
printf("Out of memory, exiting.\n");
exit(EXIT_FAILURE);
}
printf("Enter a new password: ");
while((c=getchar())!='\n'){
if (i>=n){
n *= 2;
if (!(password->pass=realloc(password->pass,
(n+1)*sizeof(char)))) {
printf("Out of memory, exiting.\n");
exit(EXIT_FAILURE);
}
}
password->pass[i++]=c;
}
password->pass[i+1]='\0';
password->length=i;
}
Run Code Online (Sandbox Code Playgroud) #include<stdio.h>
//This program is about structure and there pointer
//
typedef struct{
int i;
char c;
}str1,*strptr;
str1 str[5];
strptr *ptr;
int main(){
ptr = &str;// This is shown as incompatible type assignment **warning**
ptr->i=35; // **error**: request for member 'i' in something
//not a structure or union
ptr->c='d';//**error**: request for member 'c' in
// something not a structure or union
printf("My structure values are %d %c\n",str[0].i,str[0].c);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我运行这个程序时,会出现一个警告和两个错误.请阅读注释行以获取警告和错误.
我错过了什么?
我有一个在main中声明的二维指针数组
char* data[3][8]
Run Code Online (Sandbox Code Playgroud)
我把它传递给了一个函数
void func(char*** data)
Run Code Online (Sandbox Code Playgroud)
当我printf("%p\n", data[0]);在main函数中和函数中执行时,我0x7ffeabc27640在main和函数中得到了不同的输出(nil).虽然打印只data输出与内部相同的地址main.为什么我不能在函数中访问数组.
我试图将一个字符串数组传递给一个函数,在这个函数中对它进行一些更改,并将其传递回main()并打印它以查看更改.它没有按预期工作.请告诉我我哪里出错了.
#include <stdio.h>
#include <string.h>
#include <malloc.h>
//don't forget to declare this function
char** fun(char [][20]);
int main(void)
{
char strar[10][20] = { {"abc"}, {"def"}, {"ghi"}, {""},{""} }; //make sure 10 is added
char** ret; //no need to allocate anything for ret, ret is just a placeholder, allocation everything done in fun
int i = 0;
ret = fun(strar);
for(i=0;i<4;i++)
printf("[%s] ",ret[i]);
printf("\n");
return 0;
}
//don't forget function has to return char** and not int. (Remember char**, …Run Code Online (Sandbox Code Playgroud) 我想以一种很好的方式初始化一个指针数组..有点像
handler[numberOfIndexes] = {&bla, &ble, &bli, &blo , &blu};
Run Code Online (Sandbox Code Playgroud)
但它不会这样工作.显然,我得到一个错误,因为我试图在函数的单个指针中放置一个函数指针数组:
cannot convert ‘<brace-enclosed initializer list>’ to ‘void (A::*)()’ in assignment
Run Code Online (Sandbox Code Playgroud)
那么,这是您要测试的代码:
#include <iostream>
#include <list>
using namespace std;
class A
{
private:
void first();
void second();
void third ();
// and so on
void(A::*handlers[4])(void);
public:
A();
};
void A::first()
{
}
void A::second()
{
}
void A::third()
{
}
A::A()
{
//this is ugly
handlers[0] = &A::first;
handlers[1] = &A::second;
handlers[2] = &A::third;
//this would be nice
handlers[4] = {&A::first,&A::second,&A::third,0};//in …Run Code Online (Sandbox Code Playgroud) 我有以下课程
class a {
std::shared_ptr<b> b_ref;
public:
a(std::shared_ptr<b> b_ref) : b_ref(b_ref) {}
};
class b {
std::shared_ptr<a> a_ref;
public:
b(std::shared_ptr<a> a_ref) : a_ref(a_ref) {}
};
Run Code Online (Sandbox Code Playgroud)
现在我想创建两个相互引用的对象有没有办法做到这一点?我认为使用c风格的指针我可以做到这一点.它也适用于共享指针吗?我尝试了reset()和swap()方法没有任何成功.
std::shared_ptr<a> a_ref;
std::shared_ptr<b> b_ref;
a_ref = std::make_shared<a>(b_ref);
b_ref = std::make_shared<b>(a_ref);
Run Code Online (Sandbox Code Playgroud)
不工作.
我实际上无法弄清楚我在这里做错了什么.
所以基本上我有一个对象指针,我传递给一个函数来设置它的值以指向一个新的Object实例,但它似乎不起作用,我无法解决原因.下面是我正在尝试做的代码示例.
GetPointer(Object *pointer){
pointer = new Object();
}
in main:
Object *pointer;
GetPointer(pointer);
Run Code Online (Sandbox Code Playgroud)
我也尝试过立即初始化指针,这样可行,所以我真的无法理解上面的代码有什么问题.有什么建议?
Object *pointer = new Object();
Run Code Online (Sandbox Code Playgroud) 我正在将一个C++项目转换为Java.我有一个疑问,char**在Java中是等同于什么,是它String[]还是String[][]?