Questions tagged [pointers]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
21
votes
2answers
59k views

Difference between char and char* in c

suppose we declare two variable char s[]= "hello" char* p = "hello". what is the difference between these two?
7
votes
2answers
763 views

How can char* contain a collection of characters instead of a memory address?

I'm a little confused as to how C treats int* differently from char*. For example, in section, we see things like: int x = 5; int* ptr_x = &x; So, here, ptr_x points to the address in memory of ...
5
votes
1answer
1k views

PSET2: CRACK vs POINTERS

SPOILERS (maybe) Hi, I have just started working on crack. I have no previous experience, at least not too much but I'm trying to grasp the logic behind this and it seems my biggest issue for this is ...
2
votes
2answers
357 views

Pointer Concepts

I am problem set 6 and very confused by the use of pointers. Please tell me the difference of these things first? What is the difference between char word[length] and char *word = malloc(sizeof(char) ...
2
votes
1answer
2k views

Pointers and arrays, don't understand the relationship

I made two structs, they are very similar. The first one is a node and the second one, called hash, only contains a pointer to a node. In main I've made pointers to allocated memory for them. newNode ...
2
votes
1answer
26 views

Using * and & both works

SPOILER ALERT THIS CODE ALREADY WORKS SO DON'T LOOK IF YOU'RE STILL SOLVING I'm just wondering why my code still works even if I change fwrite(buffer, 512,1, img); to fwrite(&buffer, 512,1, img); ...
1
vote
2answers
913 views

Printf doesn't print whole string "11111"

String a, that is a pointer, contains 5 1's, but printf outputs only one 1. #define _XOPEN_SOURCE #include <unistd.h> #include <stdio.h> #include <stdlib.h> #...
1
vote
1answer
215 views

vigenere output "incompatible pointer types"

I'm trying to solve pset2 vigenere, but I'm having some troubles. I'm using two functions to adapt the key to the text and then to apply the cipher. When I run the last function, I can't get any ...
1
vote
1answer
448 views

Difference between s and *s in pointer

This code print Only first letter i.e 'H' #include <stdio.h> int main(void) { char *s = "Hello"; printf("%c\n", *s); } But this return 'Hello' #include <stdio.h> int main(void)...
1
vote
1answer
8k views

C: Vigenere - incompatible pointer types passing 'char **' to parameter of type 'const char *

I'm trying to iterate repeatedly over key (a word) which the user inputs on the command line. This keyword is used to encipher a word which the user inputs when prompted by the word 'plaintext'. I ...
1
vote
1answer
105 views

pset4 resize file header question

I'm struggling a bit with pset4 changing the header info. I feel like in principle I know what I'm supposed to do but struggling a bit with syntax/pointers? Following the resize walkthrough, I know ...
1
vote
1answer
39 views

Why does this snippet of code work?

I used address of operator on top of derefrence operator. Can you please explain what is happening behind the scenes. Here is my code: #include <stdio.h> int main(void) { int arr[10]; ...
1
vote
2answers
645 views

Update a value in the function

I want to update variables relating to velocities in a different function to where it is declared. But am not fully understanding the syntax for it. I have the prototype as follows: void moveBall(...
1
vote
2answers
122 views

3 days failing to get bubblesort working .. devastated :(

i am a beginner, i got linear search working quite fast.. but then total stuck. i tried to write function test, that gives me some random numbers and sort them. I watched all the videos and thought ...
1
vote
1answer
229 views

How can I avoid segfaults when sorting my linked list?

I have been working hard with speller but I am stuck when it comes to inserting the nodes in alphabetical order. I am pretty sure I am doing something wrong, but I am not sure exactly where the ...
1
vote
1answer
2k views

Dynamic string length allocation in new_node->word; array type char [46] is not assignable

I am trying to make my node size dynamically change with the size of the word from the dictionary in the load(dictionary) function. I understand that the warning means I am trying to assign a pointer ...
1
vote
1answer
987 views

Why Am I Having a Memory Leak?

Considering this simple program, // preprocessor directives #include <cs50.h> #include <stdio.h> #include <stdlib.h> // define a struct typedef struct structure { string str; }...
1
vote
1answer
16 views

Pointers Memory Allocation

Here I have a user-defined structure "person" that stores two pointers one for the name and one for the phone number. I create an array people of length 2 of type person. Each of the ...
1
vote
1answer
33 views

Pointers to strings

I have yet another question regarding pointers to strings. I want to know why this code works fine, and by that I mean that it can change the char d to the char Q. char Name [] = "Andrew B. Mayfair"; ...
1
vote
1answer
9k views

pset1 Error: indirection requires pointer operand

Here is a snippet of my code: long long card = get_long_long(); long long *FindLength = *card; long long *FindFirstTwoDigits = *card; And here are the errors: credit1.c:8:29: error: indirection ...
1
vote
2answers
8k views

pointers and scanf : when do I use &x, *x or just x?

The following code works but why do I have to use 'x' instead of '&x' inside scanf()? ``` #include <stdio.h> #include <stdlib.h> int main(void) { char *x = malloc(sizeof(char) *...
1
vote
1answer
100 views

PSET5 Question about pointers

I had a question for dictionary.c. First, I'm still confused at why load and check imput is const char*. I understand that const char* means that the value that char* points to will never change and ...
1
vote
2answers
2k views

Checking the base address of pointer string

#include <stdio.h> #include <string.h> int main(void) { char *a="World"; char *b; printf("a=%p\n",&a); // Base address of a b=a; // Base address of a ...
1
vote
1answer
394 views

Arrays of Pointers - implicit double dereferencing

Good day people! The context is resize program in problem set 4, however, the question is general. Following is how the main function begins for the relevant program: int main(int argc, char *argv[])...
1
vote
1answer
54 views

References and Pointers

What is the difference between a reference variable ( eg int& ref) and a pointer variable ( int*pointer ).
1
vote
1answer
30 views

How to use a pointer?

I wanted to pass the value of argv to a function but it is showing error so can any body hellp me my function void init(void) { // TODO int size = *dim; int row; int column; int ...
1
vote
1answer
10k views

incompatible pointer to integer conversion

incompatible pointer to integer conversion link to my file https://ide.cs50.io/rahulatrkm/ide50#openfile-x I was geeting message like this clang -fsanitize=integer -fsanitize=undefined -ggdb3 -O0 -...
1
vote
1answer
130 views

pset 4 - recover -> invalid pointer

i am trying to solve the recover issue. When I run my programm I get: * Error in `./recover': free(): invalid pointer: 0x0000000001da5200 * Aborted I don't know how to proceed. Can someone please ...
1
vote
0answers
258 views

Pset5: questions about malloc() and calloc()

One clip of my code: #include <stdbool.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #include <stdio.h> #include "dictionary.h" // Define a struct ...
1
vote
1answer
331 views

PSET5 - Issues with initialization of pointers in LOAD function

I'm still trying to debug my load function, I rewrite my code again and again but with no result... I know we need to have specific question, but now I just have so many questions. So, I'll go with ...
1
vote
1answer
55 views

Pointer and scope

I know that when I create a pointer and use malloc() to allocate some chunk of memory, that memory is located in the heap. Where is the pointer itself stored? Is it in the stack like any other ...
1
vote
1answer
163 views

Why isn't a dereference operator used when doing File I/O in C

It might be a dumb question, but I've searched and couldn't find an answer. For example FILE *fp = fopen (argv\[1\], "w"); ... fputs (input, fp)[1]; The fp doesn't have a * in front of it. I ...
0
votes
1answer
1k views

Pset4 - Recover (usage of sprintf)

When I am working on Pset4 - Recover, I realize I do not understand the concept of array and pointer. Although I finally worked it out, I would be very grateful if you guys can explain the following: ...
0
votes
1answer
91 views

incompatible pointer types error

I'm working on lab5, inheritance, and I'm trying to decipher why I'm getting this error when trying to compile: inheritance.c:52:32: error: incompatible pointer types assigning to 'struct person *' ...
0
votes
1answer
48 views

I know that my recover is wrong, but I'm not sure what steps to take-

I 'm getting a segmentation fault for my recover code. I know that it's wrong, and I think that this boils down to me not really understanding how to apply pointers/file functions. Maybe you could ...
0
votes
1answer
352 views

Pointer passed as argument, then suddenly goes null

In the main method I declare a character pointer pCharacter. I pass this variable to the setCharacter method and assign it an address and a variable gets assigned to that address. From what I ...
0
votes
1answer
200 views

recover pset4 file handling

I got this code to work, but I have a few questions for my own clarification. On line 68 where I define the jpg title, why is it a single character? The actual title 001.jpg is much longer than a ...
0
votes
1answer
3k views

Error with comparison between pointer and integer

I'm having some problems with making the vigenere assignment from CS50. I get many errors about my comparison between pointer and integer, how can I avoid this error? I also have some problems with ...
0
votes
1answer
23 views

I have written this swap code but it is not compiling, Where's the bug

while compiling error says insert ')'at void swap(&a,&b); I can't understand this error here is the code #include <stdio.h> #include <cs50.h> void swap (int* x,int* y); int main (...
0
votes
2answers
27 views

I made this copy program as in the shorts by david but it is giving two error described below

Here is the code with error message in the comments #include <stdio.h> #include <string.h> #include <cs50.h> #include <ctype.h> int main (void) { char* s=GetString(); ...
0
votes
1answer
484 views

Why does "if (input[i] == " ")" throw an error of "incompatible integer to pointer conversion"?

This question took me long enough to figure out that I figured I would ask and answer in case some future student had the same problem. I actually had the same problem in Psets 1 and 2, but I took a ...
0
votes
1answer
141 views

Want to make sure I understand struct, malloc, and pointers and how they relate to each other

Sort of related to PSET4 resize, but actually want to make sure I understand this in general. So at first I just declared a struct BITMAPINFOHEADER called "new_bi" and dealt with it while avoiding ...
0
votes
1answer
56 views

Whats the adress of this string?

int main(void) { char* str="world"; printf("%s\n",str); } I think that i have understand the int pointers but with the chars pointers is still fuzzy.. I can't understand the above ...
0
votes
2answers
120 views

Pointers and asterisks

I searched and found Luke's very detailed reply to a similar question, however, my question (and confusion) still remain. I don't understand why we can malloc a pointer to a struct, and this also ...
0
votes
1answer
10 views

Week 4 Memory - Swap

Does the = (assignment) get me a copy of the content or points me to the same location in memory? For some reason, I end up thinking that what the code does is not setting y = 44 in a separate memory ...
0
votes
1answer
60 views

PSET4 Blur not passing Check50

My code seems to work (when visually inspecting output files), but Check50 does not pass it. I'm struggling to understand what is causing this issue despite having repeatedly read over the code and ...
0
votes
1answer
66 views

PSET2 Caesar - Stuck at 'isdigit' step: Do I need to learn about pointers, or is something else going on?

Here is what I am attempting, and my errors. I am gathering that I am not grasping the "array of strings nature of argv[], and since I am following along with the material from class, I am pretty ...
0
votes
1answer
50 views

PSET4 filters helpers.c

I need some help with pointers, mostly. I've malloc'd a BUFFER pointer to the struct data type RGBTRIPLE. Though I can't access its properties. // Reflect image horizontally void reflect(int height, ...
0
votes
1answer
46 views

CS50 Pset4 Logic Problem

I am 3 days into PSET 4 Recover. In PSET 4 we are supposed to create a program which inputs a memory card and scans through that memory card for the signatures of lost JPEG files. Once the header of a ...
0
votes
1answer
41 views

pset4 recover - how can I get last few bytes to JPG

I've finally gotten recover.c to the point where it compiles. When I run it, it gives me this error message: munmap_chunk(): invalid pointer Aborted But it also generates all fifty of the pictures! ...