Thread: Array of pointers

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    39

    Array of pointers

    Hi guys

    Its been a while since i did some see

    I just want to let the user enter in a few strings in store them in an array of pointers. Which each pointer pointing to the begining of a different string.

    Its been a while and i barley remember how. Here is what i got some how but it doesnt seem to work. Any suggestions?

    Thanks
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    
    int main (){
    
    
    char *pointer[3];
    
    printf("Please enter the string you want to deal with");
    
        scanf("%s"pointer[0]);
    
    
    printf("This is what we just stored there %s \n\n",pointer[0]);
    
    
    
    
    
    
    
    
    return 0;
    
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Allocate space for the input to be placed into.
    Either use malloc type functions or declare char arrays to hold the data.
    Or, use a mixture of the two.

    Tim S.
    Last edited by stahta01; 06-02-2012 at 02:36 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    39
    Ok i think this is where i lost

    I have done what you suggested many times but it requries a char array for every single string. Meaning i now need n number of arrays per string

    Is there any way to avoid this?

    Just declare one array a pointer array and have it point to the strings.

    Like i know you can have a 2 dem array and the array stores all the strings so i dont need a bunch of char strings?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Read this link about arrays!
    Arrays in C - Cprogramming.com

    Read link about pointers/malloc Pointers in C - Tutorial - Cprogramming.com
    Decide if you want to use malloc or not. Then, state your answer!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by automagp68 View Post
    I have done what you suggested many times but it requries a char array for every single string. Meaning i now need n number of arrays per string
    It sounds like you have not done as suggested at all as it does not require you to make any char arrays.

    Or if you're quibbling over multiple calls to malloc, what's the difference if it all works and takes very few lines of code?
    Sure, you could make just one call to malloc and plonk every string back to back in that memory block, but if you wanted to say make one of the strings longer afterwards then that isn't any good.

    Just post code for what you're trying to do. It doesn't matter if it crashes, just at least try and get it to compile, unlike what you posted already.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  2. Replies: 4
    Last Post: 07-01-2009, 01:53 PM
  3. Syntax for constant array of array pointers
    By BMintern in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 08:21 AM
  4. Replies: 2
    Last Post: 04-27-2008, 03:39 AM
  5. Replies: 1
    Last Post: 10-21-2007, 07:44 AM