NeonGlow
NeonGlow

Reputation: 1692

Pointer to a character constant

Sorry for being such a dumb here. Can't sort this out for myself.

In a header file there is a Macro like this.

#define kOID  "1.3.6.1.4.1.1.1.2.4.0"

How to declare and initialize a char pointer to this data without creating a copy of this string?

Upvotes: 0

Views: 100

Answers (4)

barak manos
barak manos

Reputation: 30136

Assuming that you're not planning to change the contents of this string, you can simply use:

char* p = kOID;

The string will reside in a read-only section of the program, so any attempt to change its contents will result with a memory access violation during runtime. So for your own safety, you should generally use:

const char* p = kOID;

Thus, any attempt to change the contents of the string pointed by p will lead to a compile-time error instead of a runtime error. The former is typically much easier to track-down and fix than the latter.

To summarize the const issue, here are the options that you can use:

      char*       p = kOID;
      char* const p = kOID; // compilation error if you change the pointer
const char*       p = kOID; // compilation error if you change the pointed data
const char* const p = kOID; // compilation error if you change either one of them

UPDATE - Memory Usage Considerations:

Please note that every such declaration may result with an additional memory usage, adding up to the length of the string plus one character, plus 4 or 8 bytes for the pointer (depending on your system). Now, the pointer is perhaps less of an issue, but the string itself might yield an extensive memory usage if you instantiate it in several places in the code. So if you're planning to use the string in various places within your program, then you should probably declare it globally in one place.

In addition, please note that the string may reside either in the code-section of the program or in the data-section of the program. Depending on your memory partitions, you may prefer having it in one place over the other.

Upvotes: 2

mesmerizingr
mesmerizingr

Reputation: 1447

To add to what has been said by others, also you can initialize your array this way:

const char some_string[] = kOID;

This is similar to const char *const some_string = kOID;. Possibly, it may lead to additional memory allocation but this depends on compiler.

Upvotes: 0

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57650

include the header file first.

#include <header.h>

Add the defined constant

char * s = kOID;

This will compile the program fine. However as kOID is a string literal it'll be saved on read only memory of your program. So if you modify the s it'll cause Segmentation fault. The get around is to make s constant.

const char * s = kOID;

Now if you compile the program compiler will check any assignment on s and notice accordingly.

a.c: In function ‘main’:
a.c:10:5: error: assignment of read-only location ‘*s’

So you'll be safe.

Upvotes: 1

Marco A.
Marco A.

Reputation: 43662

Preprocessor macros are nothing but a textual substitution. Thus if you write

  const char *pointer = kOID;

the preprocessor will substitute the text with

  const char *pointer = "1.3.6.1.4.1.1.1.2.4.0";

One thing to bear in mind is that the const specifier is necessary since once the textual substitution is made, the memory will be allocated on read-only segments.

Also be careful to have the macro visible at the point where you'd like to declare that pointer.

Upvotes: 3

Related Questions