pointers

Pointer to constant

蓝咒 提交于 2021-02-16 15:37:29
问题 It is known that it is a good practice to pass arguments (specially if they are of big size like structures) to functions by reference. And to apply the principle of "least privilege", if that function is not supposed to change the values of these passed variables then we need to pass their references as a pointer to constant. My question is, passing as a pointer to constant can not prevent changing the values within the function (You may consider the attached code), so how can we prevent

How to input strings into an array in C?

半世苍凉 提交于 2021-02-16 13:53:08
问题 I tried to get the inputs(strings) from user and store them in an array.But after I ran this code, the program instantly crashed. #include <stdio.h> int main() { int i; char *word[3]; for(i=0;i<3;i++) { printf(" Enter a word: "); scanf("%s", &word[i]); } printf("%s ", word[0]); return 0; } 回答1: In this line: scanf("%s", &word[i]); You need to make sure word[i] is pointing somewhere, and has enough space to occupy the string entered. Since word[i] is a char * pointer, you need to at some time

Why can't I dereference a pointer to an object that's an array-element using the indirection operator?

百般思念 提交于 2021-02-16 09:48:53
问题 Is it not possible to dereference a pointer to an object that's stored in an array using the indirection(dereference) operator or am I doing something wrong? #include <iostream> class A { public: virtual void test() { std::cout << "A\n"; } }; class B : public A { public: void test() { std::cout << "B\n"; } }; int main() { A* v[2]; v[0] = new A(); v[1] = new B(); v[0]->test(); *(v[1]).test(); // Error! If the arrow operator is used instead // though, the code compiles without a problem. return

How to json unmarshalling with custom attribute type in Go

一个人想着一个人 提交于 2021-02-15 07:06:42
问题 In my project, I've defined structures so that get data from JSON. I tried to use json.Unmarshal() function. But it did not work for custom type attribute. There was a structure like this: type TestModel struct { ID NullInt `json:"id"` Name string `json:"name"` } In there, NullInt type was defined with implementations of MarshalJSON() and UnmarshalJSON() functions: // NullInt ... type NullInt struct { Int int Valid bool } // MarshalJSON ... func (ni NullInt) MarshalJSON() ([]byte, error) { if

Segmentation fault when trying to use intrinsics specifically _mm256_storeu_pd()

无人久伴 提交于 2021-02-11 15:52:04
问题 Seemed to have fixed it myself by type casting the cij2 pointer inside the mm256 call so _mm256_storeu_pd((double *)cij2,vecC); I have no idea why this changed anything... I'm writing some code and trying to take advantage of the Intel manual vectorization. But whenever I run the code I get a segmentation fault on trying to use my double *cij2. if( q == 0) { __m256d vecA; __m256d vecB; __m256d vecC; for (int i = 0; i < M; ++i) for (int j = 0; j < N; ++j) { double cij = C[i+j*lda]; double

Segmentation fault when trying to use intrinsics specifically _mm256_storeu_pd()

旧巷老猫 提交于 2021-02-11 15:51:57
问题 Seemed to have fixed it myself by type casting the cij2 pointer inside the mm256 call so _mm256_storeu_pd((double *)cij2,vecC); I have no idea why this changed anything... I'm writing some code and trying to take advantage of the Intel manual vectorization. But whenever I run the code I get a segmentation fault on trying to use my double *cij2. if( q == 0) { __m256d vecA; __m256d vecB; __m256d vecC; for (int i = 0; i < M; ++i) for (int j = 0; j < N; ++j) { double cij = C[i+j*lda]; double

Segmentation fault when trying to use intrinsics specifically _mm256_storeu_pd()

旧巷老猫 提交于 2021-02-11 15:51:15
问题 Seemed to have fixed it myself by type casting the cij2 pointer inside the mm256 call so _mm256_storeu_pd((double *)cij2,vecC); I have no idea why this changed anything... I'm writing some code and trying to take advantage of the Intel manual vectorization. But whenever I run the code I get a segmentation fault on trying to use my double *cij2. if( q == 0) { __m256d vecA; __m256d vecB; __m256d vecC; for (int i = 0; i < M; ++i) for (int j = 0; j < N; ++j) { double cij = C[i+j*lda]; double

What should happen, when we try to modify a string constant?

不问归期 提交于 2021-02-11 15:08:51
问题 #include<stdio.h> #include<string.h> int main() { int i, n; char *x="Alice"; // ....... 1 n = strlen(x); // ....... 2 *x = x[n]; // ....... 3 for(i=0; i<=n; i++) { printf("%s ", x); x++; } printf("\n"); return 0; } String constant cannot be modified. In the above code *x means 'A'. In line 3 we are trying to modify a string constant. Is it correct to write that statement? When I run this code on Linux , I got segmentation fault . But on www.indiabix.com, they have given answer: If you compile

Why is string pointer in golang behavior counter-intuitive in range-loop?

北慕城南 提交于 2021-02-11 14:31:24
问题 With this code: https://play.golang.org/p/tCm1W-K-6ob This code would print: [c c c] , but [a b c] is excepted. type A struct { a *string } func f() { slist := []string{"a", "b", "c"} list := make([]*A, len(slist)) for i, v := range slist { item := &A{ a: &v, } list[i] = item } fmt.Printf("[%s %s %s]", *list[0].a, *list[1].a, *list[2].a) } func main() { f() } Why the list is not ["a", "b", "c"] ? What happened to the range or the &string ? 回答1: https://play.golang.org/p/utJrUmxeqfh package

Why is string pointer in golang behavior counter-intuitive in range-loop?

久未见 提交于 2021-02-11 14:28:19
问题 With this code: https://play.golang.org/p/tCm1W-K-6ob This code would print: [c c c] , but [a b c] is excepted. type A struct { a *string } func f() { slist := []string{"a", "b", "c"} list := make([]*A, len(slist)) for i, v := range slist { item := &A{ a: &v, } list[i] = item } fmt.Printf("[%s %s %s]", *list[0].a, *list[1].a, *list[2].a) } func main() { f() } Why the list is not ["a", "b", "c"] ? What happened to the range or the &string ? 回答1: https://play.golang.org/p/utJrUmxeqfh package