Pointer Arithmetic

Pointer Arithmetic


I am a programmer beginning to learn C++. I wrote this chunk of code:

/////////////////////////////////////////////////////////////// Get to do some pointer arithmetic!/////////////////////////////////////////////////////////////#include #include const char NL = '
';const char TB = '	';char* ReverseString(char *pStr);void main(){	char *str = "This is a wild test string...";	char *ch = &str[0];	for (unsigned i=0;i

This code compiles without any error warnings. However, when I execute the code, the program crashes with an Access Violation error. In debugging I found that the offending line is in the ReverseString function, and I encounter the Access Violation error at the following statement:

	*f = *b;

I compiled the same program in C++ Builder 3.0. The program compiled successfully, and on execution it worked perfectly fine. So, the problem lies with Visual C++ 6.0 compiler.

Let me provide the system configuration.

Platform: Intel

Operating System: Windows 2000 RC2

Development Environment: Visual C++ 6.0 Enterprise Edition SP3

Why is this error happening?


Your reverse() function attempts to modify a const array of chars, which is a disallowed operation. Remember that every string literal is a constant, and so is str in this statement:

char *str = "This is a wild test string...";

First, never use plain char * when pointing to a literal string. Use const char * instead:

const char *str = "This is a wild test string...";

Now if you try to compile the const version, your compiler will flag the reverse() call in main() as an error. What you should do is use an array of char rather than a pointer:

char str[] = "This is a wild test string...";

This should be OK.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes