0

I'm trying to gain an intuition for how the stack structure works but for some reason any time I try to print from charElements, my program crashes and I have NO clue why. This is the error I keep getting: (its at a breakpoint) while (i-- && *p). But I have no clue whats wrong with how I declared everything. Any thoughts?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

typedef struct Stack
{
    int capacity;       // max # of elements the stack can hold
    int size;           // current size of the stack
    int *elements;      // the array of elements
    char *charElements; // the array of chars
}Stack;

Stack * createStack(int maxElements)
{        
    // Create a Stack         
    Stack *S;        
    S = (Stack *)malloc(sizeof(Stack));        
    // Initialise its properties         
    S->charElements = (char *)malloc(sizeof(int)*maxElements);
    S->elements = (int *)malloc(sizeof(int)*maxElements);   
    S->size = 0;        
    S->capacity = maxElements;        
    /* Return the pointer */        
    return S;
}




int main()
{

    Stack *S = createStack(60);     

    char registerNames[63] = {"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};

    // if the user input a a string



    S->elements[S->size++] = 1;
    S->elements[S->size++] = 2;
    S->elements[S->size++] = 3;

    S->charElements[S->size++] = *registerNames;

    printf("%d \n", S->elements[0]); 
    printf("%d \n", S->elements[1]); 
    printf("%d \n", S->elements[2]);  
    printf("%d \n", S->size); 
    printf("%s \n", S->charElements[3]);


    system("pause");

    return 0;
}
dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
ellie0414
  • 83
  • 1
  • 3
  • 8
  • 1
    I can see no `while (i-- && *p)` in your code – John Dvorak Oct 25 '12 at 04:38
  • FYI: in C, don't cast the result from `malloc` (i.e. you should use `S = malloc(...)` instead of `S = (Stack *)malloc(...)`. See also this question: http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – nneonneo Oct 25 '12 at 04:38
  • 1
    `printf("%s")` expects a `char*`, you're giving it a single `char`. Other things you should look into: zero out `charElements` after allocating it. You're overallocating `charElements` - try `malloc(sizeof(char) * maxElements)` instead. And don't cast the return value of `malloc`. – DCoder Oct 25 '12 at 04:39
  • it brings up that break point in an output.c file. the issue seems to be with the printf("%s \n", S->charElements[3]); line because when I comment it out the program runs. – ellie0414 Oct 25 '12 at 04:39
  • `registerNames` is an array, not a pointer to one. You shouldn't dereference it. – John Dvorak Oct 25 '12 at 04:40

2 Answers2

2
printf("%s \n", S->charElements[3]);

S->charElements[3] is a char, not a char *. So, when you try to print it out, you will dereference a bad memory address and crash.

Use printf("%c \n",S->charElements[3]); to print out the char at that position instead.

Also, note that

S->charElements[S->size++] = *registerNames;

is going to copy only one character from registerNames, since it treats that as a char dereference. If you wanted to copy the string, use strcpy instead (but make sure you have enough space!!)

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • so i changed that and its printing out this weird block symbol? no clue how or why... – ellie0414 Oct 25 '12 at 04:54
  • working now! thanks for the help. i guess actually the better question is this. im not used to working with char arrays. (normally strings). how would i edit my stack's declaration to take in char arrays rather than just chars. i need that value to hold strings. – ellie0414 Oct 25 '12 at 04:57
  • It already holds char arrays, you just have to copy them in the right way. You can use `strlen` to find out how long a string is (so you can adjust the stack count), and `memcpy` or `strcpy` to copy a whole string to the array. – nneonneo Oct 25 '12 at 05:00
  • sorry i phrased that weird. i need it to hold an array of strings, so basically an array of char arrays. – ellie0414 Oct 25 '12 at 05:09
  • In that case, you want a `char **` (logical, isn't it?). Good luck! – nneonneo Oct 25 '12 at 05:11
  • wow :) that actually worked! Thanks so much. this was driving me crazy. i dont normally work with c (its been a while) but i need to review it. kind of in the "ask a million dumb questions" stage haha. – ellie0414 Oct 25 '12 at 05:14
0

Problem is in this statement

printf("%s \n", S->charElements[3]);

change this to

printf("%c \n", S->charElements[3]);

Your program won't crash.

printf with %s requires a null terminated string. You do not have a null terminated string with S->charElements[3]. It's just a single char.

user93353
  • 13,733
  • 8
  • 60
  • 122
  • sorry to respond to multiple posts with the same question, but i needed to actually use strings (char arrays) in charElements, how would i declare that in the Stack struct and createStack functions? – ellie0414 Oct 25 '12 at 05:00