• C

Simple question on pointers and strings

void getStringComponents(char *tokenString)
   char keyValue1[30];
   char keyValue2[30];
   int numToks;
   numToks= sscanf(tokenString,"%s %s",keyValue1,keyValue2);
   numToks= sscanf(tokenString,"%s %s",&keyValue1,&keyValue2);

Both sscanf seem to work fine. Is CASEB incorrect for some reason? I thought &keyValue1 will refer to same address as keyValue1 being &keyValue1[0]. Pls explain
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


> Both sscanf seem to work fine. Is CASEB incorrect for some reason? I thought
> &keyValue1 will refer to same address as keyValue1 being &keyValue1[0]. Pls explain

Almost...  "keyValue1" by itself is the same as &(keyValue1[0]).  That is, it points to the first element in the array.

&keyValue1 takes the address of the pointer that points to the first element in the array.

Hope that helps,
sambha03Author Commented:
So isnt that the same? Cos the pointer  that points to the first element of the array is keyvalue1 itself. It seems to wrk fine.
No, they aren't the same.

Say you do this:
     char SomeString[50];

That will allocate space for 50 chars.  Let's just say that these 50 chars occupy addresses 10 through 59.

Now, you have the following values:
     SomeString              = 10 (the location of the first element)
     SomeString[0]          = The VALUE stored at address 10
     &(SomeString[0])     = The address of the VALUE stored at address 10 (which is 10, obviously)
     &SomeString            = The address of SomeString

In your CASEB, how are keyValue1 and keyValue2 defined?

The Five Tenets of the Most Secure Backup

Data loss can hit a business in any number of ways. In reality, companies should expect to lose data at some point. The challenge is having a plan to recover from such an event.

sambha03Author Commented:
They are local variables into which I am trying to read the input string that is passed.
keyValue1 is an array.  When you pass it to a function, it automatically gets converted to a pointer to the first element of the array.

&keyValue1 is a pointer to the array.  It's already a pointer, so it doesn't get converted when you pass it to a function.  The array is located at the same place as its first member, so the pointer to the array and the pointer to the first element have the same value, which is why they are interchangeable to sscanf.  But they don't have the same type to the compiler.  You can check this with this code:

      char ar[30];
      char* p1 = ar;
      char* p2 = &ar;       //Error!  Pointer to array of char is not the same as pointer to char.
      ar (*p3)[30] = ar;  //Error!
      char (*p4)[30] = &ar;    // OK--p4 is a pointer to an array.

Also note that this distinction is specified for ISO C, but an old enough compilermay be more lenient.

So while both forms will work with sscanf, CASEA is technically more correct.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Yeah, even if CASEB works, you should do it the way CASEA is done because it is more correct.  CASEB may seem to work, but it could cause weird random results at some point.

One more point to add

when u declare an array like char a[10] OR char[] = "HelloWorld";

a is actually a const pointer. Thus the compiler won't allow to have it's addrss

However if u have an actual char *, i.e. char *p = new char[10] ;

Then p is NOT a const pointer so u can take the address of p



An array is not a pointer.  Admittedly, the design of C makes this confusing, because in many cases it lets you use an array as a pointer or a pointer as an array, or it automatically converts an array reference to a pointer to the array's first element.  But still they are different types of objects.  A pointer is an address, while an array is a sequence of objects of the same type.  Pointers are typically always the same size, no matter what they address.  The size of an array varies depending on what type of objects it contains and how many there are.

For a more detailed explanation, see this tutorial:


The compiler has no objection to taking the address of a const pointer that is an lvalue, that is, one that has an address.  For example, the following code compiles:

      const char c = 'a';
      const char* const p1 = &c;
      const char* const * const p2 = &p1;

I do not agree with u efn

The compiler does not do anything special to let u treat an array as a pointer

Actually spaking, as I posted earlier, an array is actually handled as a const pointer

And the const modification, C "DOES NOT" handle const properly

Also try this

char *p = new char[10] ;
strcpy ( p, "HelloWorld" ) ;

char * const  ptr = p ; // You WON'T be able to change what ptr points to once u initialize

char * ptr1 = p ;  // You would  be able to change what ptr points to

char arr[10] = "HelloWorld" ;

Although u can access arr as a pointer but u cannot make arr point to some other memory location

i.e. arr is a const pointer

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.