pointers

Is C++ Array passed by reference or by pointer?

寵の児 提交于 2021-01-18 05:57:36
问题 In school, our lecturer taught us that the entire array was passed by reference when we pass it to a function,. However, recently I read a book. It says that arrays are passed by pointer by default when passing the entire array to a function. The book further mention that " passing by pointer is very similar to passing by reference ", which means that passing by pointer and passing by reference are actually different. It appears that different source stated differently. So my question is: In

Can't randomize cards. Some of them are duplicates

萝らか妹 提交于 2021-01-07 06:36:54
问题 I'm new to C, I am trying to randomize 16 cards. In the longer comments I wrote something that I don't have much clear... Btw this is the code: #include <stdio.h> #include <stdlib.h> #include <time.h> #define FACES 4 #define SUITS 4 void shuffle(char *wFace[], char *wSuit[], char *wMixed[][20]); // prototype size_t checker(char *wwMixed[][20], size_t current); // prototype int main(){ char *face[] = {"Jack", "Queen", "King", "Ace"}; char *suit[] = {"Hearts", "Spades", "Diamonds", "Clubs"};

Can't randomize cards. Some of them are duplicates

限于喜欢 提交于 2021-01-07 06:36:41
问题 I'm new to C, I am trying to randomize 16 cards. In the longer comments I wrote something that I don't have much clear... Btw this is the code: #include <stdio.h> #include <stdlib.h> #include <time.h> #define FACES 4 #define SUITS 4 void shuffle(char *wFace[], char *wSuit[], char *wMixed[][20]); // prototype size_t checker(char *wwMixed[][20], size_t current); // prototype int main(){ char *face[] = {"Jack", "Queen", "King", "Ace"}; char *suit[] = {"Hearts", "Spades", "Diamonds", "Clubs"};

meaning of these pointers, pointer-to-pointer, function pointer and array pointer

故事扮演 提交于 2021-01-07 02:42:40
问题 I'm a university student, our teacher told us to tell the meaning of these pointers, but I only manage to figure out some of them: 1. int *p1; 2. int *p2[10]; 3. int (*p3)[10]; 4. int (*p4)(); 5. int **p5(); 6. int (**p6)[10]; 7. int (**p7)(); 8. int *(*p8)(); 9. int (*p9[10])(); 10. int **p10[10]; This is what I've figured out so far: p1 is a pointer to an int p2 is an array of 10 int pointer p3 is a pointer that point to a static-array with 10 elements p4 is a function pointer p5 is not a

meaning of these pointers, pointer-to-pointer, function pointer and array pointer

痴心易碎 提交于 2021-01-07 02:40:55
问题 I'm a university student, our teacher told us to tell the meaning of these pointers, but I only manage to figure out some of them: 1. int *p1; 2. int *p2[10]; 3. int (*p3)[10]; 4. int (*p4)(); 5. int **p5(); 6. int (**p6)[10]; 7. int (**p7)(); 8. int *(*p8)(); 9. int (*p9[10])(); 10. int **p10[10]; This is what I've figured out so far: p1 is a pointer to an int p2 is an array of 10 int pointer p3 is a pointer that point to a static-array with 10 elements p4 is a function pointer p5 is not a

meaning of these pointers, pointer-to-pointer, function pointer and array pointer

[亡魂溺海] 提交于 2021-01-07 02:39:33
问题 I'm a university student, our teacher told us to tell the meaning of these pointers, but I only manage to figure out some of them: 1. int *p1; 2. int *p2[10]; 3. int (*p3)[10]; 4. int (*p4)(); 5. int **p5(); 6. int (**p6)[10]; 7. int (**p7)(); 8. int *(*p8)(); 9. int (*p9[10])(); 10. int **p10[10]; This is what I've figured out so far: p1 is a pointer to an int p2 is an array of 10 int pointer p3 is a pointer that point to a static-array with 10 elements p4 is a function pointer p5 is not a

Permute array to order of selection in Josephus problem

你说的曾经没有我的故事 提交于 2021-01-07 01:26:25
问题 How can we write code to permute an array to the order of selection in the Josephus problem? Given a sequence of items (such as an array of char * ) and a skip distance d , we skip d items from the start, take that item to be the first in the output sequence, then skip d further items, take the resulting item to be the second in the output, and continue in this way. When we reach the end of the sequence, we wrap to the beginning, continuing to count. Only items remaining in the array are

What is the point of specifying the size of an array in a parameter declaration?

白昼怎懂夜的黑 提交于 2020-12-30 07:25:57
问题 #include <stdio.h> int a[] = {1,2}; void test(int in[3]){ // } int main() { test(a); return 0; } In the above code int in[3] is the same as int *in . The number 3 doesn't really do anything and it's not even the correct size, but even so the compiler doesn't complain. So is there a reason this syntax is accepted in C or I'm missing a functionality? 回答1: When an array parameter declaration contains a constant size, the only purpose it can serve is as documentation for readers, by indicating to

Is incrementing/decrementing or adding an integer value to a pointer that is not pointing to an element in a sequence Undefined Behavior?

一曲冷凌霜 提交于 2020-12-29 03:51:25
问题 I know that pointers (to array element) and iterators can be incremented/decremented to walk a sequence of elements and can jump back-and-for elements in the sequence. But what will happen if I increment a pointer to a single object or add to it an integer value? is it undefined behavior or it is OK but we cannot access that memory? int x = 551; int* p = &x; ++p; --p; std::cout << *p << '\n'; Because I've already read that we should not increment/decrement a pointer that doesn't point to an

Argument passing by reference to pointer problem

谁都会走 提交于 2020-12-28 21:10:12
问题 Every time I try to compile my code I get error: cannot convert parameter 1 from 'int *' to 'int *&' The test code looks like this: void set (int *&val){ *val = 10; } int main(){ int myVal; int *pMyVal = new int; set(&myVal); // <- this causes trouble set(pMyVal); // <- however, this doesn't } I'd like to call that function in a single shot without creating a pointer somewhere only to pass it. And as pointers don't have constructors, something like this can't be done: set(int*(&myVal)); Is