C++ C++;定义类型的练习

C++ C++;定义类型的练习,c++,pointers,C++,Pointers,我一直在做一些书本上的练习,不知你能否告诉我这些练习是否正确。这不是家庭作业,我只是在练习。我已经评论了我应该做什么以及我的实际代码 #include <iostream> #include <string> using namespace std; //int main(){} // 1 char *ptc; //pointer to char int Array[10];

我一直在做一些书本上的练习,不知你能否告诉我这些练习是否正确。这不是家庭作业,我只是在练习。我已经评论了我应该做什么以及我的实际代码

#include <iostream>
#include <string>
using namespace std;

//int main(){}

//  1
char *ptc;                              //pointer to char
int Array[10];                          //array of 10 ints
int (&arrayRef)[10] = Array;            //ref to Array
string *pts;                            //pointer to a array of strings
char** pptc;                            //pointer to pointer to char
const int const_int =0;                 //constant integer
const int* cpti;                        //constant pointer to a integer
int const* ptci;                        //pointer to constant integer

// 3
typedef unsigned char u_char;           //u_char = 2;
typedef const unsigned char c_u_char;   //c_u_char = 2;
typedef int* pti;                       //pti = &Array[0];
typedef char** tppc;                    //ttpc = ptc; ?
typedef char *ptaoc;                    //pointer to array of char
typedef int* pta;                       //array of 7 pointers to int ?
pta myPTA = (int *)calloc(7, sizeof(int));
typedef int** pta2;                     //pointer to an array of 7 pointers to int ?
pta2 mypta2 = &myPTA; 
/* ??? */                               //array of 8 arrays of 7 pointers to int

// 4
void swap1(int *p, int *q)              //this should swap the values of p & q but the last line isn't working q = &aux??
{
    int aux;                            //int a = 5, b = 8;
                                        //swap1(&a, &b);
    aux = *p;
    *p = *q;                            //it returns 8 and 8
    q = &aux;
}

int main()
{
}
为什么函数swap1不能工作?

ad 1)

要声明一个包含8个数组和7个指向int的指针的declare数组,必须键入以下内容:

int* arr[8][7];
广告2)

交换函数不工作,因为您正在设置指向函数局部变量的指针

一个小小的改变,一切都会顺利进行:

void swap1(int *p, int *q)              //this should swap the values of p & q but the last line isn't working q = &aux??
{
    int aux;                            //int a = 5, b = 8;
                                        //swap1(&a, &b);
    aux = *p;
    *p = *q;                            //it returns 8 and 8
    *q = aux;   // <-- notice change here!
}
void swap1(int*p,int*q)//这应该交换p&q的值,但最后一行不起作用q=&aux??
{
int aux;//int a=5,b=8;
//swap1(a和b);
aux=*p;
*p=*q;//它返回8和8

*q=aux;//最好用你面临的实际问题来重新表述一下(关于:出现了否决票)@DrewDormann:这是一个让你的堆栈损坏的好方法。我从自己的痛苦经历中知道;)@DrewDormann:你是对的,真丢脸……我没有想到我忽略了什么
void swap1(int *p, int *q)              //this should swap the values of p & q but the last line isn't working q = &aux??
{
    int aux;                            //int a = 5, b = 8;
                                        //swap1(&a, &b);
    aux = *p;
    *p = *q;                            //it returns 8 and 8
    *q = aux;   // <-- notice change here!
}