Thread: pushing strings into a stack

  1. #1
    Registered User
    Join Date
    Jan 2012

    pushing strings into a stack

    I am using the STACK ADT which means the data values in the stack are void pointers.

    I am having trouble trying to push strings into the stack because of these void pointers.

    I understand how int types and char types can be pushed through:
    int* data;
    data = (int*)malloc(sizeof(int));
    scanf("%d", data);
    pushStack (stack, data);
    I am getting confused because I can't declare
    char* data
    since I have to read in a user's input so i declared
    char data[100];
    char ** dataptr;
    scanf("%99[^\n], input);
    and from then on I am clueless as to what to do

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Following a similiar pattern as with your example with int:
    char* data;
    data = (char*)malloc(sizeof(*data) * 100);
    scanf("%99[^\n]", data);
    pushStack (stack, data);
    Note that you don't need to cast the return value of malloc.
    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. pushing of arguments into the stack?
    By sanddune008 in forum C Programming
    Replies: 41
    Last Post: 07-09-2009, 01:16 PM
  2. Pushing a Queue Onto Stack
    By programming1985 in forum C Programming
    Replies: 4
    Last Post: 10-22-2008, 07:44 PM
  3. Pushing a Queue onto a Stack?
    By MiroMage in forum C Programming
    Replies: 5
    Last Post: 10-14-2008, 09:23 PM
  4. Cost of pushing variable onto stack
    By C3Pnuts in forum C++ Programming
    Replies: 5
    Last Post: 04-09-2007, 11:37 AM
  5. stack and strings...
    By QuickSilver in forum C Programming
    Replies: 2
    Last Post: 11-28-2001, 04:06 PM