-2

I have this code in c and it's working properly.

char* str2 = string + (str_len - end_len);

str_len and end_len are two integers and string is another char pointer string.

I don't understand why I am able to add an integer to a character pointer string.

Shouldn't this give a type error?

  • 2
    Read about pointer arithmetic. You say it works, but works for what? – Retired Ninja May 20 '23 at 03:04
  • 1
    Remember: if `a` is a pointer and `i` is an `int` (or another integer type), then `a[i] == *(a + i)` — so the addition of integers and pointers is legitimate. – Jonathan Leffler May 20 '23 at 03:24
  • ok i'll read about pointers. It's just that I had never seen an integer being added to a string like this. – Antas Sharma May 20 '23 at 03:46
  • there's no string in C. `char*` is just an array. There's no string operations except several functions that work on null-terminated strings stored in a char array – phuclv May 20 '23 at 03:54
  • clang, I think, has a -Wstring-plus-int option to generate warnings. Not sure where it's documented though. To me it's perfectly good code. – Allan Wind May 20 '23 at 04:53
  • @RetiredNinja guess – Antas Sharma May 20 '23 at 05:12
  • We should not have to gues, @AntasSharma. Ideally, your question should contain an MCVE ([Minimal, Complete, Verifiable Example](https://stackoverflow.com/help/mcve) — or MRE or whatever name SO now uses) or an SSCCE ([Short, Self-Contained, Correct Example](http://sscce.org/) — the same idea by a different name). Our assumptions about what you mean could be horribly wrong — we don't know the types of any of the three variables `string`, `str_len`, `end_len`. We can make intelligent guesses, but we should not have to guess. – Jonathan Leffler May 21 '23 at 17:23
  • 1
    @phuclv: there are strings in C — see the C Standard (C11): [§7.1.1 Definitions of terms ¶1](http://port70.net/~nsz/c/c11/n1570.html#7.1.1) where it says _"A string is a contiguous sequence of characters terminated by and including the first null character. "_. C does not have a full-blown string type, but it most definitely has strings. – Jonathan Leffler May 21 '23 at 17:26
  • 1
    See also [With arrays, why is it the case that `a[5] == 5[a]`?](https://stackoverflow.com/q/381542/15168) and [Pointer arithmetic](https://stackoverflow.com/q/394767/15168). – Jonathan Leffler May 21 '23 at 17:35
  • @JonathanLeffler I didn't declare many variables in this snippet but I have mentioned their data types. Also, I feel like people are overthinking this. This was probably not a question that I should have asked here. – Antas Sharma May 22 '23 at 02:06

1 Answers1

3

str2 will be assigned the pointer value of string + (str_len - end_len). If offset = str_len - end_len < 0 or offset > size of string (array) + 1 then it's undefined behavior (note: sizeof "hello " counts the trailing '\0' for a string hence the -1). Here's an example:

#include <stdio.h>

int main() {
    char string[] = "hello world";
    char *str2 = string + (sizeof "hello " - 1);
    printf("%s\n", str2);
}

prints:

world
Allan Wind
  • 23,068
  • 5
  • 28
  • 38