0

So I'm pretty new to programming and had a quick question about the order in which you declare/instantiate in your code. I'll try and be as specific and clear as possible.

I understand the appropriate places to declare/instantiate in terms of scope. But I was trying to run a certain program and after successfully compiling it I received a runtime error of:

null pointer passed as argument 2, which is declared to never be null 

I ran the debugger and noticed that a string I had declared but not yet assigned (call it string a) became null randomly. So the program would crash when it went to use it in a loop.

However, I knew the only change I had made was that instead of hardcoding a particular variable with a value that the string used in its assignment nested in the proceeding loop (which by the way was hardcoded outside of int main and ran fine), I instead instantiated a character array (let's call it char array b) in int main to allow for some variability given a user's input. This instantiation came in the line of code ABOVE the string's declaration (for no particular reason but just by chance). But both were in the same scope.

Before (WITH ERROR):

char array b[] = {};
string a;


If ()
{
    for()
    {
        //string a = (equation containing char array b);
        //returns value of string a;
    };
};

After a lot of frustration I just happened to switch the order of the string declaration and the array instantiation and now my program runs fine.

Now (WITHOUT ERROR):

string a;
char array b[] = {};

If ()
{
   for()
   {
       //string a = (equation containing char array b);
       //returns value of string a;
   };
};

I've been searching to see if declaration/instantiation order within the same scope matters when compiling but I can't seem to find a definitive answer. My gut tells me that because string a ultimately depends on char array b's value that I have to declare string a first. But why would instantiating char array b first automatically make string a null if I'm not yet "using" the string until the loop?

I want to understand the concept for future purposes. Hope that wasn't too vague and makes some sort of sense. Thanks in advance!

1 Answer 1

1

One thing first: If you declare a variable inside a function, and don't give it a value immediately, its initial value is undefined. Initially most of those variables start as all-zeroes (in case of a pointer, that's NULL, the null pointer), but there's no guarantee unless you initialise them explicitly. Global variables are initialised to all-zeroes implicitly, unless you say otherwise.

This means in most cases you have to actively give your variables a value before using them, otherwise results are undefined.

Now to your question.

I don't know where in the course you are, but to really understand strings in C, you have to have a basic understanding of pointers.

Pointers are variables just storing a memory address, and the type of the pointer tells the compiler how to interpret it.

In C, strings are such pointers. They are variables of type char*, pointer to char (string is an alias to that).

When you declare such a variable, the compiler reserves 4 bytes on 32-bit machines or 8 bytes on 64-bit machines, just for the memory address. No memory for actual characters involved. If you do like string s = "Hello, world!";, the compiler would create that string in an area where it stores constants, and initialise s to point to that address. When you do like string s = get_string("What's your name?");, that's similar. get_string allocates some memory, returns its address, and you copy the address into s. Through some "magic" I'm not advanced enough to understand, the cs50 library seems to keep track of that memory and frees it later so you don't have to.

Interestingly, arrays also behave like pointers (just using an array variable is equivalent to a pointer to the first element). On declaration of an array, there's space allocated for actual characters, but you can treat the array as if it were a pointer, and you can treat a pointer as if it were an array.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .