Click here to Skip to main content
15,887,214 members
Everything / Pointers

Pointers

Pointers

Great Reads

by honey the codewitch
Pointers don't have to be black magic.

Latest Articles

by honey the codewitch
Pointers don't have to be black magic.

All Articles

Sort by Score

Pointers 

2 Oct 2022 by OriginalGriff
You don't allocate any memory to q or p, nor do you assign them any value. So when you try to access them, what you get is just a memory access at random and your app crashes. BUt you should have spotted that for yourself, and you would have if...
2 Oct 2022 by merano99
If the warnings were switched on with your compiler it would have warned you, that the uninitialized local variables "p" and "q" are used. if (q[k] >= 'a' && q[k]
24 Oct 2022 by Patrice T
Quote: Why this programming is acting like a pointer without taking the address of the variable? char* ptr = str; // ^^^ Because it takes the address of the variable here !
24 Oct 2022 by OriginalGriff
The C Language specification says that the name of an array is a pointer to the first element of that array. So this line:char* ptr = str; Copies a address from one pointer to another. It also means that these two are equivalent: char c = *ptr; ...
16 Feb 2023 by OriginalGriff
Start by thinking "how would I do that manually?" Write the number on a blackboard then take the lowest digit out and write that down, then rub it out. If there are any digits left you do it again until there aren't. So you need three things: 1)...
17 Feb 2023 by OriginalGriff
You allocate a block of memory (20 chars worth) and tell the system that that is a set of pointers to chars - because Str is a pointer to a pointer to a char. Which is effectively a pointer to a string. Think of Str as a array of pointers to...
14 Mar 2023 by Richard MacCutchan
I just built that code at LeetCode and got a different error message: Quote: Line 36: Char 13: runtime error: variable length array bound evaluates to non-positive value -1428798312 (solution.cpp) SUMMARY: UndefinedBehaviorSanitizer:...
14 Mar 2023 by merano99
Just like Richard, I can't understand the given error message with main() here. The code shown here could report an error here: int arr[n]; 1> error C2057: Constant expression expected. 1> error C2466: Assignment of an array of constant size 0...
24 Jun 2022 by OriginalGriff
C doesn't have any concept of strings past "string literal": a sequence of characters enclosed in double quotes. So when you are traversing a char array, you need to know either how long the string is, or look for a string terminator character....
27 Jun 2022 by steveb
*ptr is dereferencing. Dereferencing is used to access or manipulate data contained in memory location pointed to by a pointer. *(asterisk) is used with pointer variable when dereferencing the pointer variable, it refers to variable being...
22 Aug 2022 by Greg Utas
You can do this within the same program (process), but most platforms prevent one process from reading the data of another process. To do that, you'd have to use a system call to make the memory accessible to another process.
22 Aug 2022 by CPallini
Quote: Can a pointer access based on a known memory address? Yes, provided they are in the same process. If you need to access it from another process then you are out of luck.
21 Sep 2022 by Patrice T
if( digit == total) An array of digits do not make an integer. Program for Armstrong Numbers - GeeksforGeeks[^] Hint: a modulo (%) allow you to extract the unit digit from an integer. int Unit= 153 % 10;
2 Oct 2022 by merano99
Numbers like 10 and 15 are better with names, it makes it easier to read. That memory must be allocated for all strings has already been written. You could get the memory with only one malloc at a time and then allocate only pointers. It would be...
2 Oct 2022 by CPallini
If you just need to 'abbreviate' the input, then you might do it in place, without messing with pointers. #include #include int main() { int i = 0; char line[50]; fgets(line, sizeof(line), stdin); while (line[i])...
6 Oct 2022 by Richard MacCutchan
Unfortunately this is a feature of the scanf function. The function scans the input for information separated by whitespace, which it uses to delimit fields. If a newline character is left in the input stream, then the next scanf request will...
3 Nov 2022 by CHill60
I couldn't get this to work at all using gets - in fact I got a compiler warning telling me not to use it. However, the syntax for gets is char *gets(char *str) and you are passing it a char **str Try replacing gets(category); with scanf("%s",...
3 Nov 2022 by CPallini
Try #include #include #include #define CHARS 30 int main() { int c, categories; printf("how many types/categories of meds you want?\n"); scanf(" %d", &categories); printf(" %d", categories); char *...
3 Nov 2022 by Richard MacCutchan
You have two problems: 1 the use of scanf to read values will leave a line end in the input buffer, which causes the next input command to read the wrong data. You need to clear the buffer after the call to scanf: char dummy[4]; gets(dummy); ...
6 Nov 2022 by merano99
As Richard has already correctly pointed out, this line does not work: gets(category); Here you want to read in several lines. But the command obviously always overwrites the same place, and one that is not intended for that. The gets()...
24 Feb 2023 by CPallini
That is how bubble sort works, see, for instance this page: Bubble sort in C | Programming Simplified[^]. Such code, however, is flawed. For instance, when swapping triangles it doesn't swaps the triangle areas as well. Moreover it performs...
27 Jun 2022 by Sreenath Sreenath
\\ #include #include int main() { int num=10; int *ptr; *ptr=# printf("%d",*ptr); } \\ What I have tried: if i declare *ptr=&num it is showing an error (INT FROM INT MAKES INTEGER FROM POINTER WITHOUT A CAST) if i declare int...
27 Jun 2022 by OriginalGriff
Pointers are a little complicated. When you write int num = 10; you are declaring a variable that holds an integer, and assigning it a value: 10 You can do maths with that, and print it if you want: int num = 10; num = num + 100; printf("num =...
22 Aug 2022 by Josh_0101
Hi. I'm trying to use a pointer by accessing the memory address which I only know the hexadecimal value. Is it possible to do that? For example, I know the address of the pointer is 0x7ffee96f6d3c and I want to manually access it outside from an...
22 Aug 2022 by OriginalGriff
Just to add to what Greg and Carlos have said ... A hexadecimal address in a pointer is not a "real" memory address - the OS maps the real memory allocated to the process to a virtual address which is used by the pointers within the app. So...
22 Aug 2022 by Rick York
How is it that you "know" that address? Normally, addresses of data items will change every time a program runs. If it is a physical address then you need to have acquired it through a driver and even it can change every time your program runs....
22 Aug 2022 by Patrice T
Quote: I got error: invalid conversion from ‘long int’ to ‘int*’ :( Modern OS are using 64 bits pointers, but integers are commonly 32 bits. So conversion of a 64 bits pointer to a 32 bits integer lead to a loss of data. You may need to use a 64...
11 Sep 2022 by Everything Select
PLEASE HELP!!! This code is supposed to run like the basic snake game (except that I haven't coded the game-over conditions yet). But what is happening is that the body length doesn't increase more than 1 (i.e., the 1st time the snake eats food,...
11 Sep 2022 by Rick York
In my cursory look at your code, I see a few issues. The first is the use of so many global variables. I understand they are needed on occasion but at the least, given them descriptive names that are longer than two letters. When I use a...
21 Sep 2022 by Sharyar Javaid
/* Write a program to check if a given number is Armstrong number or not. */ #include #include #include main() { int *digit; int size, total , i; total =0; printf(" how many digit number you want to...
21 Sep 2022 by OriginalGriff
To add to what Patrice and jeron have - rightly - said: Compiling does not mean your code is right! :laugh: Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language -...
2 Oct 2022 by Parth Vijay
Hey, I 'am a beginner in C language. problem 1: Have i used pointer p and q properly. problem 2: After taking the input health care the program just crashes. health care after this the program crashes. What I have tried: #include ...
7 Oct 2022 by Parth Vijay
Question: Write a program to take data of student as input by the help of structure student variable. Do some changes with it.(by using pointers) And print it. Problem 1; I was practicing the structures in C...
7 Oct 2022 by k5054
You should be aware that fflush(stdin) as per the C standard is undefined behavior, and should probably be avoided. If you are using Linux, take a look at getline(3) - Linux manual page[^] which will read an entire line of input at once,...
7 Oct 2022 by Rick York
The previous answers give the necessary information. I would summarize by stating you should pick one way to obtain your input and then stick with it. In my opinion, fgets is the best function to use if you accept your input one line at a time...
7 Oct 2022 by merano99
Since C++ was also asked for as a solution, you could also solve it this way. #define STRLEN 20 typedef struct { char name[STRLEN]; int roll; char gender; } student; void change( student &std); void display( student &std) { std::cout
24 Oct 2022 by Pococurante Bird
I am a beginner. As far as I know, to use a pointer first you have to give the new variable a address of the memory and then you can call the variable with * this symbol and modify it. #include int main() { ...
6 Nov 2022 by Sharyar Javaid
#include #include int main() { int i, types; i=0; printf("how many types/categories of meds you want?\n"); scanf(" %d", &types); printf(" %d", types); char * category[types]; printf("\nPlease...
6 Nov 2022 by Frozen Frostings
As the title mentioned, I have to remove adjacent duplicates in linked list such that if input is 'google', output should be 'le'. I'm supposed to code it in C. I've written 70% of the code, except that I don't know how to continuously loop till...
6 Nov 2022 by Richard MacCutchan
Use a true false flag. At the beginning of each search for duplicates, set it equal to 'false'. Whenever you find a duplicate, set it to 'true'. At the end of each loop if the value is still 'false', then you did not replace any duplicates. If it...
16 Feb 2023 by KarstenK
Try with starting some Learn C++ tutorial or some video tutorial. I strongly recommend that you take the time to read my tips to learn from my experiences in that industry. some tips: use functions and classes with understandable names and...
20 Feb 2023 by Addy__0
#include #include #include int main() { char **Str; Str= (char**)calloc(20,sizeof(char)); Str[0]= (char*) calloc(10, sizeof(char)); Str[0]= "Hello, world"; printf("%s", *(Str+0)); } What I have...
18 Feb 2023 by Addy__0
#include #include #include char **StringArray(char **arr, int size); int main() { char **Str; int n= 5; int i; Str= (char**)calloc(5,sizeof(char)); Str[0]= (char*) calloc(10, sizeof(char)); ...
17 Feb 2023 by CPallini
Quote: Str= (char**)calloc(5,sizeof(char)); That should beStr= (char**)calloc(5,sizeof(char *)); Might be there are other errors, as well. Anyway the code is not robust against user input.
17 Feb 2023 by OriginalGriff
Probably because it doesn't compile: you declare StringArray as returning achar** - but the function body does not have a return statement. If code doesn't compile, then it doesn't generate an EXE file - so what you are running is the previous...
18 Feb 2023 by merano99
Currently, this is C source code. The other programming languages according to the tag are irrelevant. There are several errors: // Str = (char**)calloc(5, sizeof(char)); need space for pointer, use n for size! Str = (char**)calloc(n,...
20 Feb 2023 by Fred van Lieshout
Just keep in mind that everything takes up memory, including pointers. It can be handy to let a pointer (to a pointer) point to something else. Also, the line where memory is allocated to store the actual string, is not needed. Str[0]= (char*)...
24 Feb 2023 by OriginalGriff
Because you index on j + 1 inside the loop. Run the code in the debugger and watch what happens - you'll see what I mean. And anyway, it a basic homework question and you wrote the code so why don't you understand it already?
14 Mar 2023 by Addy__0
#include #include #include #include int* twoSum(int* nums, int numsSize, int target, int* returnSize); int* twoSum(int* nums, int numsSize, int target, int* returnSize) { int i,j, k= 0; for(i=0;...
19 Jun 2022 by honey the codewitch
Pointers don't have to be black magic.
2 Oct 2022 by Richard MacCutchan
You cannot read (or copy) text data into arrays that do not exist. Your allocation of an array of pointers is correct, but you then need to initialise each pointer, so that it actually points to some memory space, something like: for (i = 0;...
24 Jun 2022 by Sreenath Sreenath
1)how (s[i]!=0) evaluates? 2)how does *(s+i) works? 3) how does i[s] works? please help me with these douts What I have tried: #include #include int main() { char s[]="peak"; int i=0; while(s[i]!=0) { ...
2 Oct 2022 by Parth Vijay
Problem 1: I'am not sure if i have used array of character pointers correctly or not. Problem 2: The use of scanf()? Can we input a string in string[i](which is actually a element on array of character pointer)?(You can ignore the use of...
16 Feb 2023 by Ernest Nowalk
Write a C++ program that prompts a user to enter a four-digit integer and prints the number in reverse. HINT: Print one digit at a time, starting from the right of the number. Sample run of program: Enter a four-digit number: 3412 Your number...
24 Feb 2023 by Addy__0
struct triangle { int a; int b; int c; }; typedef struct triangle triangle1; void sort_by_area(triangle1* tr, int n) { int i,j,temp; int *p=(int *) calloc(100, sizeof(int)); int *s=(int *) calloc(100, sizeof(int)); ...