stack

This is a discussion on stack within the C Programming forums, part of the General Programming Boards category; Code: typedef struct{ char string[20]; }cell; typedef struct{ cell cell1[20]; int stptr; }stack; push(stack *s_p,char *str){ (s_p->stptr)++; memcpy ((s_p->(cell1[stptr]))->string),str,strlen(str)+1); } ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    155

    stack

    Code:
    typedef struct{
               char string[20];
    }cell;
    
    typedef struct{
          cell cell1[20];
          int  stptr;
    }stack;
    
    
    
    push(stack *s_p,char *str){
    (s_p->stptr)++;
    memcpy ((s_p->(cell1[stptr]))->string),str,strlen(str)+1);
    }
    
    pop(stack *s_p,char *str){
    strcpy (str,(s_p->(cell1[stptr]))->string));
     (s_p->stptr)--;
    }

    the idea was to make a string stack,but this has so many errors,can anyone correct them for me please?
    Last edited by qqqqxxxx; 02-07-2006 at 10:54 PM.

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    What are the errors?

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    memcpy ((s_p->xxxx(cell1[stptr]))->string),str,strlen(str)+1);



    xxxx identifier expected

    (s_p->xxxxxstptr)--;


    xxxxx error:"=" or ";" or "," expected(there r a few more of this kind)

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    You are doing ->string when it should be .string
    Last edited by bithub; 02-07-2006 at 11:06 PM.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    Code:
    typedef struct{
               char string[20];
    }cell;
    
    typedef struct{
          cell cell1[20];
          int  stptr;
    }stack;
    
    
    
    push(stack *s_p,char *str){
    (s_p->stptr)++;
    memcpy (((s_p->(cell1[s_p->stptr])).string),str,strlen(str)+1);
    }
    
    pop(stack *s_p,char *str){
    strcpy (str,((s_p->(cell1[s_p->stptr])).string));
     (s_p->stptr)--;
    }


    ok i have replaced the-> by a . and corrected a few ( missing errors,but that has not helped the situation much.
    Last edited by qqqqxxxx; 02-07-2006 at 11:17 PM.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    now it is just showing two errors


    memcpy (((s_p->xxxxx(cell1[s_p->stptr])).string),str,strlen(str)+1);
    xxxxx identifier expected

    strcpy (str,((s_p->xxxx(cell1[s_p->stptr])).string));
    xxxx identifier expected



    please help.where do i get the expected identifier from?
    Last edited by qqqqxxxx; 02-07-2006 at 11:18 PM.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    memcpy (((s_p->xxxxx(cell1[s_p->stptr])).string),str,strlen(str)+1);
    xxxxx identifier expected






    ok corrected the error,
    memcpy (((s_p->cell1[s_p->stptr]).string),str,strlen(str)+1);



    had to remove the brackets around cell1[s_p->stptr]

    ok this done,so why was the compiler complaining b4,what was it assuming when i had those extra brackets on?

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    You stuck in all those worthless parethesis which are just confusing you. Delete all the parenthesis except for the ones surrounding the parameters:
    Code:
    memcpy (s_p->cell1[s_p->stptr].string,str,strlen(str)+1);

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    Code:
    memcpy (s_p->cell1[stptr], str, strlen(str)+1);
    
    strncpy (str, s_p->cell1[stptr].string,
         strlen(s_p->cell1[s_p->stptr])+1);
    Call me crazy, but that should work.
    "It compiled, let's ship it!"

    Guitar Australia
    my site for some easy tutorials.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > memcpy ((s_p->(cell1[stptr]))->string),str,strlen(str)+1);
    How about simplyfying the thing into a few more steps, you're just confusing yourself

    char *to =
    char *from =
    size_t amount =
    memcpy ( to, from, amount );

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Call me crazy, but that should work.
    It will work, but it is a bad way of doing it. What happens if later on he decides to add another data member to the cell structure? All of a sudden half his code doesn't work anymore.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Call me crazy, but that should work.
    Not if strncpy finds more characters than is specified by its third argument.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
        char array[] = "Hello, World!";
    
        puts(array);
    
        strncpy(array, "hippo", 6);
        puts(array);
    
        strncpy(array, "hippopotamus", 6);
        puts(array);
    
        return 0;
    }
    Output:
    Code:
    Hello, World!
    hippo
    hippopWorld!
    If strncpy() finds the number of characters it was looking for, it doesn't add a NULL terminator. To fix that, add array[6] = 0.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 01:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 09:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 05:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21