Thread: assign value of pointer array

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    168

    assign value of pointer array

    Code:
     char *name[4];
     int i = 0;
     while ( i < 4)
     {
         int k = 0;
         while (k < 4)
          {
                *(name[i]+k) = 'c';
                k++;
           }
         
         i++;
      }
    result: error!

    why?
    Last edited by zcrself; 08-17-2009 at 01:53 AM.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Because name[4] is out of bounds.

    You might want to look-up what i++ and k++ actually mean.
    More specifically, what the Boolean expression (i++ < 4) means...
    Code:
    int i = 3;
    if(i++ < 4)
       printf("I solved my own question\n");

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    13
    Not sure what you are trying to do but the code is attempting to insert values at name[4] which is outside the array index

    [edit: Beaten to the punch by zacs7]
    Last edited by strider1974; 08-17-2009 at 01:51 AM.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by zacs7 View Post
    Because name[4] is out of bounds.

    You might want to look-up what i++ and k++ actually mean.
    More specifically, what the Boolean expression (i++ < 4) means...
    Code:
    int i = 3;
    if(i++ < 4)
       printf("I solved my own question\n");
    Code:
     char *name[4];
     int i = 0;
     while ( i < 4)
     {
         int k = 0;
         while (k < 4)
          {
                *(name[i]+k) = 'c';
                k++;
           }
         
         i++;
      }

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by strider1974 View Post
    Not sure what you are trying to do but the code is attempting to insert values at name[4] which is outside the array index

    [edit: Beaten to the punch by zacs7]
    Code:
     char *name[4];
     int i = 0;
     while ( i < 4)
     {
         int k = 0;
         while (k < 4)
          {
                *(name[i]+k) = 'c';
                k++;
           }
         
         i++;
      }

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    Code:
    char *name[4][4];
     int i = 0;
     while ( i < 4)
     {
         int k = 0;
         while (k < 4)
          {
                *(name[i]+k) = 'c';
                k++;
           }

    i++;
    }




    //now u r code is oky.....u r using one dimention pointer array but u using two loop nested it not work....

    using two dimention then try.....

  7. #7
    1337
    Join Date
    Jul 2008
    Posts
    135
    Quote Originally Posted by karimmughal View Post
    Code:
    char *name[4][4];
     int i = 0;
     while ( i < 4)
     {
         int k = 0;
         while (k < 4)
          {
                *(name[i]+k) = 'c';
                k++;
           }

    i++;
    }




    //now u r code is oky.....u r using one dimention pointer array but u using two loop nested it not work....

    using two dimention then try.....
    That is 3 dimension
    Code:
    char *name[4][4];

  8. #8
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by zcrself View Post
    Code:
     char *name[4];
     int i = 0;
     while ( i < 4)
     {
         int k = 0;
         while (k < 4)
          {
                *(name[i]+k) = 'c';
                k++;
           }
         
         i++;
      }
    result: error!

    why?
    What are you trying to achieve? name[i] is an address, adding k to it will try to access any random memory which results in segf.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Everyone who posted and modified the original in the beginning needs to take a crash course in C strings.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Everyone who posted and modified the original in the beginning needs to take a crash course in C strings.
    Everyone? I only see one poster that attempted to modify the original code (karimmughal's post). Everyone else simply pointed out the 2 mains issues with the code.
    bit∙hub [bit-huhb] n. A source and destination for information.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Everyone who modified it, yes. I see that there is only one aside from the OP, so that's a total of two who needs to take a crash course.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    The only problem I see from the original is that there was no storage allocated for name strings. There was never any bounds issue.

  13. #13
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by nonoob View Post
    The only problem I see from the original is that there was no storage allocated for name strings. There was never any bounds issue.
    I believe the code in the original post was edited.
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 07:33 AM
  2. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-01-2007, 11:26 PM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. Replies: 6
    Last Post: 02-27-2006, 03:11 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM