[Solved] swapping bytes in a file in c++ [closed]

Create two buffers of length 100 bytes each, say A and B. Read 100 bytes from file to A (assuming that the file cursor points to the beginning of the file). Seek to file length n-100. Read 100 bytes from file to B. Again, seek to file length n-100. Write 100 bytes from A to … Read more

[Solved] What is the difference between assigning a pointer to variable address and explicitly to memory address?

Those are both undefined behavior. You are trying to modify memory that you did not allocate. The second is even less safe, because you are assuming a is going to be allocated to that address every time, which is absolutely not a safe assumption. 3 solved What is the difference between assigning a pointer to … Read more

[Solved] Segmentaion fault in C

int *ptr; is a pointer to an interger, but you never initialized it. The value of ptris not defined so this is undefined behavior. To make your code work, the value of ptr has to be an address of a variable with type int. *ptr and **p_ptr tries to read the value where ptr refers … Read more

[Solved] In C++, why should I use “new” if I can directly assign an integer to the pointer without using “new”? [closed]

int *ptr_a = 1;doesn’t create a new int, this creates a pointer ptr_a and assigns a value of 1 to it, meaning that this pointer will point to the address 0x00000001. It’s not an integer. If you try to use the pointer later with *ptr_a = 2, you will get a segmentation fault because the … Read more

[Solved] Confusion regarding Handling of Different Pointers by Compiler

Maybe a concrete example would help. Try this: #include <stdio.h> const double step_size = 5.0; // degrees of arc struct Location { double latitude; double longitude; }; void go_north(struct Location *const loc) { loc->latitude += step_size; } int main() { struct Location there; there.latitude = 41.0; there.longitude = -101.5; go_north(&there); printf(“Lat. %6.1f, long. %6.1f.\n”, there.latitude, … Read more

[Solved] Write a program to elaborate the concept of function overloading using pointers as a function arguments? [closed]

As the comments stated, it’s not entirely clear where your problem is. It would be nice if you included an example for what you want to understand better, anyway, here’s an example: #include <iostream> void foo(int x) { std::cout << x << std::endl; } void foo(int* x) { std::cout << (*x + 1) << std::endl; … Read more

[Solved] C++ Why is my program throwing an exception?

You made variable with name that matches type name (string variable, string type?). Plus there is issue that you return pointer to object with local scope of life. That is UB. Using iterators your function would work like this: const string shortest_string(initializer_list<string> strings) { if(!strings.size()) return string(); auto shortest_one = strings.begin(); for (auto it = … Read more

[Solved] C program with pointer

In theory you can, by simulating individual data structures or even the entire memory (static data, heap and stack) with arrays. But the question is whether this is very practical; it may involve having to rewrite every pointer-based standard library function you need. Anyway, there’s a nice explanation on Wikipedia: It is possible to simulate … Read more

[Solved] Why pointer is not NULL after using delete in C++

It’s because when you say delete p you’re deleting a pointer to memory which completely erases the reference to the new memory you allocated. When you say if p==NULL you’re checking to see if the pointer was set to null when in fact the memory that it was pointing to was de-allocated so the pointer … Read more