Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Mgoo12t

New Coder
I am struggling with the difference in array incrementation.
For example if I had an array. char* arr = ["a", "b", "c", "d", "e", "f"]
and if given arr[0] then the element that it is reffering to is "a", and arr[i +2] is "c".
But if the incrementation was
Code:
arr[i]+2
, then what is the element is it pointing to?
 
Last edited:
Your proposed declaration

C:
char* arr = ["a", "b", "c", "d", "e", "f"];

is invalid, and does not pass the compiler. What you have on the left here is a pointer to a char, and on the right an array of arrays (remember that a string, even a one=character string like "a", is an array of char). Those things are different. If an array of strings is what you want, define this one as

C:
    char arr[][2] = {"a", "b", "c", "d", "e", "f"};

which actually does compile and work. Note that for a two-dimensional array, the first dimension needs to have a fixed value (in this case 2, being one plus the length of the longest string).

Avoid the use of pointers if you can, especially if you are quite new to C. Despite that C "finds pointers and arrays very similar" (a quote from Kernighan and Ritchie) there can be subtle differences. Fully understanding arrays, double arrays, pointers and double pointers is one of the hardest things in C, and you really need to study and practice them or be prepared to be fazed by them for a long time.

As for your question: Assuming the definition is corrected to that you now do have an array of strings, the expression
C:
arr[i]+2
denotes the sum of a string and the number two. I'm not sure how C evaluates this, if it does at all, and don't really want to know. What do you want/expect it to be ? And is there a good reason to want to do this ? Note that C, being a very primitive language, will let you do almost anything... even if it's dead wrong. The results can be unpredictable or even fatal. You're supposed to know exactly what you want and how to do it. In that respect, a modern language like Java or C# is a lot easier.
 

New Threads

Buy us a coffee!

Back
Top Bottom