1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

stupid easy C question

Discussion in 'Software Development' started by TypeSK, Sep 14, 2004.

Thread Status:
Not open for further replies.
Advertisement
  1. TypeSK

    TypeSK Thread Starter

    Joined:
    Mar 16, 2002
    Messages:
    676
    please tell me this is a valid statement:

    Code:
    int main()
    {
    char *andfinder;
    
    .
    .
    .
    
    *andfinder = "Y";
    *andfinder = "\0";
    .
    .
    .
    }
    it wont compile ....

    i got a pointer cycling through a string, and when it finds a certain character (in my case, "&"), i want it to be replaced with another character .....

    as far as i know, using the * will dereference the pointer ... so you get whatever its pointing to

    for ex:

    char *ptr;

    *ptr --> |A|

    so if i use the following statement:

    *ptr = "B";

    then it should look like this

    *ptr --> |B|

    correct?

    or am i missing something?
     
  2. TypeSK

    TypeSK Thread Starter

    Joined:
    Mar 16, 2002
    Messages:
    676
    here is my code, as a reference to what im trying to do ....

    here is the code for that ...

    Code:
    int main()
    {
    
    char incmd[MAX];
    char *cmd[64];
    char *cdcmd[64];
    char *cddir[64];
    char *andfinder;
    int i, j, pid, status, ret_value, tmp;
    int isBEx = 0;
    
          j = 0;
    
    /* i is set to the # of words in the whole string
        j is just the current word it is on */
    
          while (j<i)
            {
              andfinder = cmd[j];
              while ((strcmp(andfinder, "\0")) == 1)
                {
                  if (strcmp(andfinder, "&") == 0)
                    {
                      printf("you found a AND sign\n");
                      isBEx = 1;
                      andfinder = "Y";
                      printf("%s\n", cmd[j]);
                    }
                  andfinder++;
                }
              j++;
            }
     
  3. TypeSK

    TypeSK Thread Starter

    Joined:
    Mar 16, 2002
    Messages:
    676
    anyone?
     
  4. Shadow2531

    Shadow2531

    Joined:
    Apr 30, 2001
    Messages:
    2,636
    Is that all of it?

    If not, we need all of it.

    From what you posted, MAX is undefined and i, pid, status, ret_value, tmp are uninitialized.

    incmd,cdcmd,cddir,pid,status,ret_value,tmp are not used.

    What header files did you include
    What errors did you get when you tried to compile it.

    I await the rest of the code.

    What compiler are you using?
    Does this have to be in c or will c++ do?

    Fill in the missing pieces and we'll go from there.

    test.c
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char incmd[MAX];
        char *cmd[64];
        char *cdcmd[64];
        char *cddir[64];
        char *andfinder;
        int i, j, pid, status, ret_value, tmp;
        int isBEx = 0;
        j = 0;
        while (j < i) {
            andfinder = cmd[j];
            while ((strcmp(andfinder, "\0")) == 1) {
                if (strcmp(andfinder, "&") == 0) {
                    printf("you found a AND sign\n");
                    isBEx = 1;
                    andfinder = "Y";
                    printf("%s\n", cmd[j]);
                }
                  andfinder++;
            }
            j++;
        }
        exit(EXIT_SUCCESS);
    }
    
     
  5. TypeSK

    TypeSK Thread Starter

    Joined:
    Mar 16, 2002
    Messages:
    676
    sorry i didnt post the whole thing ... but i found the solution?

    i did this:

    old::

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char incmd[MAX];
        char *cmd[64];
        char *cdcmd[64];
        char *cddir[64];
        char *andfinder;
        int i, j, pid, status, ret_value, tmp;
        int isBEx = 0;
        j = 0;
        while (j < i) {
            andfinder = cmd[j];
            while ((strcmp(andfinder, "\0")) == 1) {
                if (strcmp(andfinder, "&") == 0) {
                    printf("you found a AND sign\n");
                    isBEx = 1;
                    andfinder = "Y";
                    printf("%s\n", cmd[j]);
                }
                  andfinder++;
            }
            j++;
        }
        exit(EXIT_SUCCESS);
    }
    new::

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char incmd[MAX];
        char *cmd[64];
        char *cdcmd[64];
        char *cddir[64];
        char *andfinder;
        int i, j, pid, status, ret_value, tmp;
        int isBEx = 0;
        j = 0;
        while (j < i) {
            andfinder = cmd[j];
            while ((strcmp(andfinder, "\0")) == 1) {
                if (strcmp(andfinder, "&") == 0) {
                       printf("you found a AND sign\n");
                      isBEx = 1;
                      printf("%s\n", cmd[j]);
                    }
                  andfinder++;
                }
              j++;
            }
    
          if (isBEx == 1)
            {
              j = i;
              andfinder--;
              *andfinder = '\0';
            }
    
    basically, i took it out of the loop and did it seperately? kinda weird ......

    i did it in C ..compiling with gcc ...

    error message was something about casting from int to char or something to that sort ....


    here is my whole code, which works ... its just a basic minishell program... executes basic commands, and if needed, Ctl-C stops whatever your running (like an infinite loop) while not stopping the whole shell itself ..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <signal.h>
    #include <sys/wait.h>
    
    const int MAX = 1024;
    int bgp;
    
    void prntcmd ();
    void ctlC_h(int sig);
    
    
    int main()
    {
    
      char incmd[MAX];
      char *cmd[64];
      char *cdcmd[64];
      char *cddir[64];
      char *andfinder;
      int i, j, pid, status, ret_value, tmp;
      int isBEx = 0; /* 0 = NO executes in background 1 = executes in background*/
    
      signal(SIGINT, ctlC_h);
    
    
      while (1)
        {
    
        prt_prompt:
          printf("sh%% ");
          fgets (incmd, MAX, stdin);
    
          if (strcmp(incmd, "\n") == 0)
            goto prt_prompt;
    
          i = 0;
          cmd[i] = strtok (incmd," \n\t");
    
          while (cmd[i] != NULL) {
            i++;
            cmd[i] = strtok(NULL," \n\t");
          }
    
          j = 0;
    
          while (j<i)
            {
              andfinder = cmd[j];
              while ((strcmp(andfinder, "\0")) == 1)
                {
                  if (strcmp(andfinder, "&") == 0)
                    {
                      isBEx = 1;
                    }
                  andfinder++;
                }
              j++;
            }
    
          if (isBEx == 1)
            {
              j = i;
              andfinder--;
              *andfinder = '\0';
            }
    
          if (strcmp(*cmd, "bye") == 0)
            break;
    
            if (strcmp(*cmd, "?") == 0)
              prntcmd();
    
            if (strcmp(*cmd, "cd") == 0)
              chdir(cmd[1]);
    
            isBEx = 0;
    
          pid=fork();
          if (pid==0)
            {
              if (strcmp(*cmd, "?") != 0 && strcmp(*cmd, "cd") != 0)
                execvp(*cmd, cmd);
              exit(0);
            }
          else if (pid>0)
            {
              if (isBEx == 0)
                ret_value=wait(&status);
            }
    
        }
    
      return 0;
    }
    
    
    void prntcmd ()
    {
      printf(" ls                                 -- list files in current directory\n");
      printf(" cd <dir_name>                      -- change directory\n");
      printf(" emacs <file_name>                  -- opens emacs file editor\n");
      printf(" ps                                 -- lists current processes\n");
      printf(" mk <dir_name>                      -- creates a new directory\n");
      printf(" rm <file_name>                     -- deletes the filename\n");
      printf(" cp <old file_name> <new file_name> -- copies the file to a new file\n");
      printf(" ?                                  -- brings up this menu\n");
    }
    
    void ctlC_h(int sig)
    {
    
    }
     
  6. codejockey

    codejockey

    Joined:
    Feb 11, 2002
    Messages:
    1,405
    OK, let's start at the beginning ...

    You need to distinguish between a character and a character string. When you dereference a pointer to a character, you get a character (NOT a character string). So, for example, if you have declared a pointer to a character such as:

    char *charp = "This is a character string";

    then *charp dereferences to 'T' -- i.e., one, single character (a capital 'T'). You may, if you choose, assign this character pointer to another string (e.g., charp = "Another string"); in this case, *charp now dereferences to 'A'. The compiler treats a single character as an int (integer) whose value is the ASCII value of the character (e.g., 0x41 is 'A'). An integer is not the same as a pointer, and most compilers will complain. When you use a quoted string, the compiler translates this into a pointer reference; a statement such as *charp = "Y" attempts to assign what charp points to (a character) to a pointer reference ("Y") which generally causes problems with most compilers :))). More details available on request.

    And no -- not stupid, not easy, and a source of confusion for lots of programmers.

    Hope this helps.
     
  7. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/273742

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice