Thread: help with pointers and arrays

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    1

    help with pointers and arrays

    Hey guys, I m having trouble trying to add values manually into the array and then getting them by iterating over it by calling these functions:


    Code:
    add(STRUCT night *a, char *val, unsigned *index);

    and I get the values back by calling this function:


    Code:
    get(STRUCT night *a, unsigned index);

    Here's my code:

    Code:
    #include <stdio.h>
    #include "datarrays.h"
    
    
    int main () {
    
    struct night *a;
    
    int pos1 =1;
    int pos2 = 2;
    int *ptr1;
    int *ptr2;
    
    ptr1 = &pos1;
    ptr2 = &pos2;
    
    add(a, "hello", ptr1);
    add(a, "world", ptr2);
    
    
    /* How do I print these values back..? */
    
    printf ("%s\n", get(a, *ptr1);
    printf ("%s\n", get(a, *ptr2);
    
    
    return 0;
    }

    How do i get these values back by iterating over them instead? I get a segmentation fault. I dont want to manually get the values back (currently done).


    Any help?

    Thanks.

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    What is struct night? Note that you haven't actually allocated any space for it, you just declared a pointer of type struct night. We need to see more information.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... now show us ALL of your code... there isn't enough there to determine what the problem is.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Double Header! ... Hi Andrew...

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by CommonTater View Post
    Double Header! ... Hi Andrew...
    LOL...seems to happen a lot. Hi back Tater!
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    LOL...seems to happen a lot. Hi back Tater!
    That's why I've started calling them... seems as good a chance for a greeting as any!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers to pointers with arrays
    By Nurumla in forum C Programming
    Replies: 3
    Last Post: 07-18-2011, 11:53 PM
  2. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. Need Help with pointers and arrays
    By Dan17 in forum C++ Programming
    Replies: 6
    Last Post: 11-23-2005, 04:46 PM
  5. Pointers in Arrays?
    By mart_man in forum C Programming
    Replies: 8
    Last Post: 12-09-2002, 05:26 PM