1

I am new to programming and have faced this particular problem:

int *FindLine(char *lines[100],int line_number) {
  char **pointer=lines;
  int i,*stack,counter=0;
  stack=(int*)calloc(30,sizeof(int));
  for (i=0;i<line_number;i++)
    if (*pointer[i]=='{') { 
      stack[counter]=i+1;
      counter++;
    }
  return stack;
}

main(){
  char *line[100];
  FILE *fp;
  char FileName[20];
  char Buffer[100];
  fp=fopen(FileName,"r");

  while(fgets(Buffer,100,fp)!=NULL) {        
    line[i]=strdup(Buffer);                    
    i++;
  }            
  NumOfLines=i;
  Stack=FindLine(line,NumOfLines);
  system("PAUSE");
}

stack is supposed to have stored the number of the line each '{' appears in , instead it only stores it if '{' is the first char on the line. is there a way on C to access every single individual character of the strings pointed by the pointers in the array of pointers to strings ?

2
  • Use malloc instead of calloc. Did not calloc allocate memory on the stack (function scope memory)? Hence one cannot return it outside. Commented Apr 19, 2013 at 16:02
  • @JoopEggen - no. calloc does not allocate memory from the stack. It allocates it the from the heap. Commented Apr 19, 2013 at 16:06

1 Answer 1

2

Change

if (*pointer[i]=='{')

to

if (!strchr(pointer[i],'{'))

You may need to add

#include <string.h>

at the beginning.

pointer[i] points to the string (array of chars). *pointer[i] gives you the first character of the string. So your if condition checks only the first character.

So you have to use strchr

strchr checks if the '{' char is there anywhere in the string. returns the pointer to first occurance if found or 0/NULL if not found.

Sign up to request clarification or add additional context in comments.

Comments

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.