There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Software Development
Tag Cloud
audio blue screen boot bsod computer cpu crash dell desktop driver drivers error excel external hard drive firefox freezes freezing hard drive hardware hijack hijackthis internet internet explorer itunes laptop malware monitor mouse network networking outlook 2007 power printer problem ram router screen slow sound spyware trojan usb virus vista vista 32-bit windows windows xp windowsxp winxp wireless
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
stupid easy C question


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. Enjoy!

Closed Thread
 
Thread Tools
TypeSK's Avatar
Senior Member with 680 posts.
 
Join Date: Mar 2002
Experience: Intermediate
14-Sep-2004, 01:09 AM #1
stupid easy C question
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?
__________________
AMD Athlon64 X2 4400+
DFI LanParty nF4 SLI-DR
256MB BFG 7800GTX OC
1GB OCZ Gold 2x512 DC PC3200

It's Not That We Don't Know, It's That We Don't Do What We Do Know
TypeSK's Avatar
Senior Member with 680 posts.
 
Join Date: Mar 2002
Experience: Intermediate
14-Sep-2004, 01:10 AM #2
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++;
        }
TypeSK's Avatar
Senior Member with 680 posts.
 
Join Date: Mar 2002
Experience: Intermediate
14-Sep-2004, 11:27 AM #3
anyone?
Shadow2531's Avatar
Distinguished Member with 2,629 posts.
 
Join Date: Apr 2001
15-Sep-2004, 07:09 AM #4
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);
}
__________________
10 ? "a line as the unending horizon"
20 ? "a curve as the rolling hillside"
30 ? "a point as a distant bird"
40 ? "a ray as the rising sun"
run
TypeSK's Avatar
Senior Member with 680 posts.
 
Join Date: Mar 2002
Experience: Intermediate
15-Sep-2004, 02:16 PM #5
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)
{

}
__________________
AMD Athlon64 X2 4400+
DFI LanParty nF4 SLI-DR
256MB BFG 7800GTX OC
1GB OCZ Gold 2x512 DC PC3200

It's Not That We Don't Know, It's That We Don't Do What We Do Know
codejockey's Avatar
Senior Member with 1,410 posts.
 
Join Date: Feb 2002
20-Sep-2004, 01:47 AM #6
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.
__________________
The slowest component still sits at the keyboard.
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 11:40 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.