PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, November 1, 2022

[FIXED] Why are normal C-array indices signed while stl indices are unsigned?

 November 01, 2022     c, c++, indexing, stl     No comments   

Issue

I understand why stl indices are unsigned, because you would never have a negative index. But for normal C arrays, the indices are signed. Why is this?

If there is a good reason for C array indices to be signed, why did they decide to make stl indices different?


Solution

Array indexing in C is really just a pointer offset. x[y] is exactly the same as *(x + y). That allows you to do things like this:

int a[3] = { 1, 2, 3 };
int *p = a;                   /* p points to a[0]  */
printf("p[1]=%d\n", p[1]);    /* prints 2          */
p += 2;                       /* p points to a[2]  */
printf("p[-1]=%d\n", p[-1]);  /* prints 2          */

Which is why negative array indexing is allowed.



Answered By - dbush
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing