void add()
{
char name[50], surname[50], usern[50];
int id, birth, amount;
printf("Enter your name, surname and your year of birth:\n");
scanf("%s %s %d", &name, &surname, &birth);
printf("Enter your ID, username and your total amount:\n");
scanf("%d %s %d", &id, &usern, &amount);
const char *pass1=function(usern);
}
const char *function (char usern[50])
{
char temp;
int i=0;
int j;
j = strlen(usern) - 1;
while (i < j)
{
temp = usern[i];
usern[i] = usern[j];
usern[j] = temp;
i++;
j--;
}
return usern; …Run Code Online (Sandbox Code Playgroud) 当您获得一个指针some_queue.front(),将其分配给另一个变量,然后调用时会发生some_queue.pop()什么?谁应该清理内存?(我使用的是c ++ 98,如果确实需要,我可以使用boost智能指针)
例如:(为什么要这样做?还是不应该这样做?)
class SClass {
public:
SClass(int si): sInt(si){}
int getSInt(){ return sInt; }
private:
int sInt;
... // bunch of other complicated data types so copy might be slow
};
int main()
{
cout << "Hello World" << endl;
queue<SClass *> sq;
for(int i = 0; i < 10; i++){
SClass *sc = new SClass(i);
sq.push(sc);
}
SClass *s2 = NULL;
while(!sq.empty()){
s2 = sq.front();
sq.pop();
cout << s2->getSInt() << endl;
}
delete …Run Code Online (Sandbox Code Playgroud) 在大型单线程C++应用程序(使用GCC 4.4.7 20120313编译)中,简单指针赋值不等于原始指针值:
class DvComVectorStreamBase : virtual public std::ios
{
// some stuff here
};
class DvComVectorOStream : public DvComVectorStreamBase, public std::ostream
{
public:
DvComVectorOStream(int which = std::ios::out, size_t capacity = 0) :
DvComVectorStreamBase(which, capacity)
{}
};
class Formatter : public aStandAloneClass
{
// cruft removed
protected:
void initFunction();
ostream* mpStream;
DvComVectorOStream* mpVectorOStream;
}
Formatter:initFunction()
{
printf("mpStream %p mpVectorOStream %p\n", (void *)mpStream, (void *)mpVectorOStream);
if (mpVectorOStream == 0)
mpVectorOStream = new DvComVectorOStream(ios::out, 32768);
else
mpVectorOStream->clear();
printf("mpStream %p mpVectorOStream %p\n", (void …Run Code Online (Sandbox Code Playgroud) 我正在制作一个包含模板和类的动态数组.
这是我遇到问题的代码:
template<typename GType>
class GArray
{
GType* array_type = nullptr;
int size = 0;
public:
GArray(GType Size)
{
size = Size;
array_type = new GType[size];
for (int i = 0; i < size; i++)
array_type[i] = NULL;
}
void Push(GType Item)
{
size++;
GType* temp = new GType[size];
for (int i = 0; i < size-1; i++)
temp[i] = array_type[i];
temp[size] = Item;
delete[] array_type;
array_type = temp;
temp = nullptr;
}
GType& operator[] (int Index)
{
if (Index …Run Code Online (Sandbox Code Playgroud) 当我尝试编译时,我得到一个错误说:"解除指向不完整类型struct Freunde的指针"
那是我的结构:
typedef struct {
char *Name;
struct Freunde *next;
} Freunde;
Run Code Online (Sandbox Code Playgroud)
错误发生在这里:
while (strcmp(Anfang->next->Name, Name) != 0)
Anfang = Anfang->next;
Run Code Online (Sandbox Code Playgroud)
编辑///所以这里是我尝试运行的程序中的更多代码:
void add(Freunde* Anfang, char* Name) {
Freunde * naechster;
while (Anfang->next != NULL) {
Anfang = Anfang->next;
}
Anfang->next = (Freunde*) malloc(sizeof(Freunde));
naechster = Anfang->next;
naechster->Name = Name;
naechster->next = NULL;
}
int main() {
Freunde *liste;
liste = (Freunde*) malloc(sizeof(Freunde));
liste->Name = "Mert";
liste->next = NULL;
add(liste, "Thomas");
add(liste, "Markus");
add(liste, "Hanko");
Ausgabe(liste);
return 0;
}
Run Code Online (Sandbox Code Playgroud) #include<stdio.h>
#include<stdlib.h>
int *func(int *);
int main(void)
{
int i,size;
const int *arr=func(&size);
for(i=0;i<size;i++)
{
printf("Enter a[%d] : ",i);
scanf("%d",&arr[i]);
}
for(i=0;i<size;i++)
{
printf("%d\t",arr[i]);
}
return 0;
}
int *func(int *psize)
{
int *p;
printf("Enter the size: ");
scanf("%d",psize);
p=(int *)malloc(*psize *sizeof(int));
return p;
}
Run Code Online (Sandbox Code Playgroud)
输入大小:3
输入[0]:1
输入[1]:2
输入[2]:3
1 2 3
编辑
我已经在这里阅读了问题和答案:指向数组/指针数组的C指针消歧.那里的答案并没有解决我的问题的重点以及下面已经发布的一些答案.这是类似的,但不是重复的.这里的重点旨在集中解释如何()更改声明.链接的重点更广泛,实际上并不围绕()声明的影响和影响,例如我在下面列出的声明.
造成差异的原因是什么?
int *a[5];
int (*a)[5];
被创造了吗?
我对如何()更改声明的解释特别感兴趣.
例如:
随着1)
a[0] = malloc(sizeof(int)); //compiles
Run Code Online (Sandbox Code Playgroud)
但是2)它没有.(错误:数组类型int [5]不可分配)
问题:
1)()in 的影响是2)什么?
2)为什么宣言2会被用于宣言1?
(也就是说,为什么要使用声明2?)
我是C++和一般指针的新手,但我似乎无法解释为什么我在两个几乎完全相同的代码之一中出现分段错误.我正在做一个简单的遍历a的任务LinkedList,这是我做的第一种方式:
Node * curNode = head; //Head is the pointer to the head of the LinkedList
while(curNode) {
curNode = curNode->next;
}
Run Code Online (Sandbox Code Playgroud)
这会产生所需的结果并成功遍历整个LinkedList.但是,这会引发Segmentation fault错误:
Node * curNode = head;
while(curNode->next != NULL) {
curNode = curNode->next;
}
Run Code Online (Sandbox Code Playgroud)
将条件更改为已转换的布尔值或!= nullptr不执行任何操作,因此条件不会引发true空指针.然而,我似乎无法弄清楚为什么我得到一个Segmentation fault,似乎适当的循环条件确保我不访问任何空指针.
我有这个代码:
char str [80];
int i;
printf ("Enter your family name: ");
scanf ("%79s",str);
printf ("Enter your age: ");
scanf ("%d",&i);
Run Code Online (Sandbox Code Playgroud)
为什么scanf()的string保存str而对于int它被发送到pointer of i?
我用C写了一个简单的程序(C中的新手)
当我尝试编译这个程序时,它会被编译.
创建指向int'p'的指针并存储变量'a'的地址
当我derefernce指针它显示值0但我从未分配任何值
到我的变量那么0来自哪里?如果我们写
int a;
它,它甚至在为它分配任何值之前在内存中分配空间.
#include <stdio.h>
int main()
{
int a;
int *p = &a;
printf("%d\n", *p);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器:clang