kr-c

K&R Qsort example with Pointers and Arrays confusion

我是研究僧i 提交于 2019-12-30 00:40:08
问题 I find it difficult to understand the following snippet of code. I understand the pointer to function mannerism showed, but where I find confusion is in the indicated lines. void qsort(void **v, int left, int right, int (*comp) (void *, void *)) { int i, last; void swap(int **v, int i, int j); if (left >= right) /* do nothing if array contains */ return; /* fewer than two elements */ swap(v, left, (left + right)/2); /* move partition elem */ [1] last = left; /* to v[0] */ [2] for (i = left +

At least the first 31 or 63 characters of an internal name are significant? [closed]

*爱你&永不变心* 提交于 2019-12-19 04:59:47
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . Here's a direct quote from the Book (K&R, 2nd ed, p. 35): "At least the first 31 characters of an internal name are significant. For

UINT_MAX the same as ULONG_MAX in C

对着背影说爱祢 提交于 2019-12-13 05:45:32
问题 While solving exercises from the K&R C book, I stumbled upon the exercise 2.1. At first I got as UINT_MAX as -1 , but then I used the %u placeholder, but now its giving me the same number as ULONG_MAX . In the book in Appendix B, they say that UINT_MAX should be 65535 and ULONG_MAX should be 4294967295 , but when running the exercise, its giving me for both UINT_MAX and ULONG_MAX as 4294967295 . Why is that? 回答1: First of all, the right way to print an unsigned long is not %u but %lu . Second

Problem with example 1.5.2 in K&R book on C

∥☆過路亽.° 提交于 2019-12-09 12:57:44
问题 I'm teaching myself C with K&R and am stumped by one of the examples in the book. I compile the code exactly as it is written in the example but it does not do what the authors say it will. The program is supposed to count characters. The code given is as follows: #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc=0; while (getchar() != EOF) ++nc; printf("%ld\n", nc); } For it to compile I replace main() with int main(). But I assume that is not relevant to

K&R C Exercise Help

寵の児 提交于 2019-12-09 12:36:50
问题 I've been going through the K&R C Programming Language book and I'm stuck on Exercise 2-6 which reads: Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged. I'm having trouble understanding the exact thing they're looking for me to do. I looked at a possible answer here, but I still don't really understand. I think it's the wording that's throwing me off. Can anyone maybe explain what they

Writing into c-string

倾然丶 夕夏残阳落幕 提交于 2019-12-07 16:31:22
问题 my code segfaults and I don't know why. 1 #include <stdio.h> 2 3 void overwrite(char str[], char x) { 4 int i; 5 for (i = 0; str[i] != '\0'; i++) 6 str[i] = x; 7 } 8 9 int main(void) { 10 char *s = "abcde"; 11 char x = 'X'; 12 overwrite(s, x); 13 printf("%s\n", s); 14 return 0; 15 } The gdb debugger tells me, that problem is on the line 6, where I want to store a char, into c-string (if I use lvalue pointer dereferencing, it's the same problem.) This is what he says: (gdb) run Starting

pointer to array of integers and normal array of integers

坚强是说给别人听的谎言 提交于 2019-12-06 02:09:27
问题 In KR C book page 112 it says that following: int (*arr1)[10]; is a pointer to an array of 10 integers. I don't get what's difference between above and: int arr2[10]; 1- Isn't arr2 itself a pointer to array of 10 integers? (Because name of an array is a pointer itself.) 2- If the name of an array is the array address and pointer to that array, then both arr1 and arr2 are pointer to array of integers, isn't this true? 回答1: Isn't arr2 itself a pointer to array of 10 integers? No, it's an array.

k&r exercise confusion with bit-operations

一个人想着一个人 提交于 2019-12-04 03:31:25
问题 The exercise is: Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged. My attempt at a solution is: #include <stdio.h> unsigned setbits(unsigned, int, int, unsigned); int main(void) { printf("%u\n", setbits(256, 4, 2, 255)); return 0; } unsigned setbits(unsigned x, int p, int n, unsigned y) { return (x >> (p + 1 - n)) | (1 << (n & y)); } It's probably incorrect, but am I on the right path

correctly declaring the main() function in ANSI C [duplicate]

感情迁移 提交于 2019-12-03 17:48:13
问题 This question already has answers here : What should main() return in C and C++? (17 answers) Closed 5 years ago . The C standard say: The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters: int main(void) { /* ... */ } or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared

Problem with example 1.5.2 in K&R book on C

泄露秘密 提交于 2019-12-03 15:20:18
I'm teaching myself C with K&R and am stumped by one of the examples in the book. I compile the code exactly as it is written in the example but it does not do what the authors say it will. The program is supposed to count characters. The code given is as follows: #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc=0; while (getchar() != EOF) ++nc; printf("%ld\n", nc); } For it to compile I replace main() with int main(). But I assume that is not relevant to the question. The program compiles and runs fine. But it simply does not count characters as it was