Thread: What's wrong with my simple C code?

  1. #16
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    You don't understand C pointers.
    You don't understand C memory allocation.
    You don't understand C pointer accessing.
    The correct access to an element in a stringarray (char**) is StrArr[i] not StrArr+i, see:

    Ideone.com | Online C Compiler & Debugging Tool

  2. #17
    Registered User
    Join Date
    Jan 2011
    Posts
    6
    BillyTKid, from what I understand I can use the the StrArr+i to move up the array. I've seen some examples using this method.

  3. #18
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by godmoney View Post
    BillyTKid, from what I understand I can use the the StrArr+i to move up the array. I've seen some examples using this method.
    Well... there is a reason C provides the [] indexing of arrays... The array[x] syntax takes the size of the array elements into account. The array+x method does not...

    Just because you can do something does not mean you should.

  4. #19
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > The array[x] syntax takes the size of the array elements into account. The array+x method does not...
    Mmm, want to try that again?

    Code:
    #include <stdio.h>
    
    int main()
    {
      int arr[10] = { 1, 2, 3 };
      arr[3] = 4;
      printf("%d\n", *(arr+3) );
      return 0;
    }
    This prints 4, as it should.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #20
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    > The array[x] syntax takes the size of the array elements into account. The array+x method does not...
    Mmm, want to try that again?
    Ummmm.... maybe not?

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by godmoney
    BillyTKid, from what I understand I can use the the StrArr+i to move up the array. I've seen some examples using this method.
    Sure, that allows you to obtain a pointer that points to a different element in the array, i.e., the element at index i. However, to actually access the element at index i, you must dereference the pointer result, i.e., StrArr+i should be *(StrArr+i). But *(StrArr+i) is equivalent to StrArr[i], so you might as well use StrArr[i].

    Consequently, just writing StrArr+i is not wrong because the "array+x method does not" take "the size of the array elements into account": it is wrong because it does not access the element that you want to access.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. whats wrong with this very simple code?
    By sway1 in forum C++ Programming
    Replies: 2
    Last Post: 07-06-2008, 12:18 PM
  2. What's wrong with this code?
    By Luciferek in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2008, 12:02 PM
  3. Whats wrong with this simple code?
    By o14v in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2008, 01:46 PM
  4. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM