2

I am trying to find the location of an element in the array. I have tried to use this code i generated

for(i=0;i<10;i++)
    {
    if (strcmp(temp[0],varptr[i])==0) j=i;
    }

varptr is a pointer which points to array var[11][10] and it is by the definition *varptr[11][10]. I have assigned strings to var[i] and i want to get the "i" number of my element NOT THE ADRESS.

Thanks for any comment.

EDit: temp is also a pointer which points to the string that i want to check. Also i am using the 2D array for keeping variable names and their address. So yes i want to keep it inside a 2D array. The question is this code is not working at all, it does not assigns i to j, so i wonder where is the problem with this idea? writing a "break" does not change if the code works or not, it just optimizes the code a little.

Full Code:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

double atof(char*);
int main(void)
{
    char in[100], *temp[10],var[11][10],*varptr[11][10];
    int i,j, n = 0,fullval=0;
    double val[11];
    strcpy(var[11], "ans");
    for(i=0;i<11;i++)
    {
        for(j=0;j<10;j++) varptr[i][j]=&var[i][j];
    }
START:
    printf("Enter the expression: ");
    fflush(stdout);
    for(i=0;i<10;i++) temp[i]=NULL;

    if (fgets(in, sizeof in, stdin) != NULL)
    {
        temp[0] = strtok(in, " ");

        if (temp[0] != NULL)
        {
            for (n = 1; n < 10 && (temp[n] = strtok(NULL," ")) != NULL; n++)
                ;
        }
        if (*temp[0]=="quit")
        {
            goto FINISH;}

        if (isdigit(*temp[0]))
        {

            if (*temp[1]=='+') val[0] = atof(temp[0])+atof(temp[2]);
            else if (*temp[1]=='-') val[0] = atof(temp[0])-atof(temp[2]);
            else if (*temp[1]=='*') val[0] = atof(temp[0])*atof(temp[2]);
            else if (*temp[1]=='/') val[0] = atof(temp[0])/atof(temp[2]);
            printf("%s = %f\n",var[11],val[0]);
            goto START;

        }
        else
            if (temp[1]==NULL) //asking the value of a variable
            {
            for(i=0;i<10;i++)
            {
            if (strcmp(temp[0],varptr[i])==0) j=i;
            }
            printf("%s = %d\n",var[j],val[j]);
            goto START;
            }
            if (*temp[1]==61)
            {
            strcpy(var[fullval], temp[0]);
            if ((temp[3])!=NULL)
            {
            }
            val[fullval]=atof(temp[2]);
            printf("%s = %f\n",var[fullval],val[fullval]);
            fullval++;
            goto START;
            }
            if (*temp[1]!=61)
            {


            }

    }
    getch();
FINISH:
    return 0;


}
4
  • 6
    Not enough code. Post more, including variable definitions and initializations. Commented Jun 4, 2010 at 7:14
  • If you somehow found the index and stored it to j, break the loop. You won't find any better result. Commented Jun 4, 2010 at 7:24
  • Do you mean that you've defined varptr as char *varptr[11][10]? That's a (2-dimensional) array of pointer-to-char, which I don't think is what you want. Commented Jun 4, 2010 at 7:48
  • try splitting up your function in smaller functions, makes the code easier to read. it will also help you avoid using goto's Commented Jun 4, 2010 at 8:51

3 Answers 3

2
int i;
int found = 0;
for (i = 0 ; i < 11 ; i++)
{
    if (strcmp(temp,var[i]) == 0) 
    {
           found = 1; 
           break;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

One comment: you can exit the loop as soon as you find your string.

#define NOT_FOUND (-1)

int j = NOT_FOUND;
int i;
for (i = 0 ; i < 11 && j == NOT_FOUND; i++)
{
    if (strncmp(temp,var[i], 10) == 0) // Nick D's comment
    {
        j = i;
    }
}

Another comment:

I was unable to understand how varptr and var relate to each other (please show definitions). I have used var in the above on the assumption it is defined:

char var[11][10];

also

char temp[10];

1 Comment

Do you mean why i < 11 && j == NOT_FOUND? It's because if the first element matches your string, there's no point in bothering to check the other 10.
1
j=-1;
for(i=0;i<10;i++) 
{ 
  if (strcmp(temp[0],varptr[i])==0) {j=i;break;} 
}//strcmp is not safe, try use strncmp

3 Comments

It's not a string if it doesn't have a '\0' though.
So if it was passed pointers to areas of memory that did not have \0 it would compare forever, or cause an access violation. strncmp limits the maximum compare length
If you give a pointer to memory that is not a valid, zero-terminated string as input to a str-function, all hope is lost, anyway. If you can't be sure that your input is a valid string, what makes you sure that you give the correct n to the strn-functions? Especially when you can't use strlen for the very same reason?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.