Why char** dynamic_string_array(int ROWS, int SIZE) doesn't work properly?

c

    Next

  • 1. Bitwise Operators: Aplication
    I'm learning to program in C and any tutorial or book that I read likes to briefly touch on birdies operators and then move on without giving any sort of example application of them. Call me what you will but I cannot seem to see the purpose for bitwise operators. Especially the operators bitwise OR ( | ) and bitwise AND ( & ), I'm just not getting it. I have searched around and really haven't found anything that gave explanation to why these were necessary to learn or even adequately describe what they do. Therefor any explanation or recommendation of the bitwise operators would be greatly appreciated by me. Thanks in advanced. Nori **If I'm off topic tell me**

Why char** dynamic_string_array(int ROWS, int SIZE) doesn't work properly?

Postby dddddddd2444444 » Tue, 15 Mar 2005 21:28:33 GMT

Hi,please help...
It works fine when I define a 2-D array like char code[ROWS][SIZE].
But it won't work when I try to define the array dynamically using a
function. It just crashes.
Does anyone know why?
The compiler i'm using is Dev c++.


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

int file_length(FILE *file);
void down_string(char *p);
char* issubstring(char *str1,char *str2);
char** dynamic_string_array(int ROWS, int SIZE);

int main(void)
{
  int row,n,i,coursefound=0,ROWS,SIZE,test;

  FILE *datafile;
  datafile=fopen("3rdyear.csv", "rb");
  ROWS=file_length(datafile);

  printf("Has %d lines\n",ROWS);

  char **Code,**Course,**ClassSize,**Time1,**Time2,**Room,c;
  char search[30];


  Code=dynamic_string_array(ROWS,SIZE);
  Course=dynamic_string_array(ROWS,SIZE);
  ClassSize=dynamic_string_array(ROWS,SIZE);
  Time1=dynamic_string_array(ROWS,SIZE);
  Time2=dynamic_string_array(ROWS,SIZE);
  Room=dynamic_string_array(ROWS,SIZE);


  for (row=0; row <ROWS; row++) {
  test=fscanf(datafile, "%[^,],%[^,],%[^,],%[^,],%[^,],%[^\n]\n",
  Code[row],Course[row],ClassSize[row],
  Time1[row],Time2[row],Room[row]);


  printf("row=%d,scanf converted %d (%s,%s,%s,%s,%s,%s)\n",
  row, test,

Code[row],Course[row],ClassSize[row],Time1[row],Time2[row],Room[row]);
}


printf("Please enter a name\n>");
  scanf("%s",search);


  for(i=1; i<ROWS; i++) {
      if(issubstring(Course[i],search))  {
                coursefound=1;
                printf("Course %s found!!\nTime 1 is %s\nTime2 is
%s\nVenue:%s",Course[i],Time1[i],Time2[i],Room[i]);

           }
   }
   if(coursefound==0) printf("No such Course!.\n");
  fclose(datafile);


 return EXIT_SUCCESS;

}


void down_string(char *p)  //turns uppercase letters in a string to
lowercase
{
    int i;
    for(i=0;p[i]!='\0';i++)
    {
        if((p[i]>='A')&&(p[i]<='Z')) p[i]+=32;
    }
}


char* issubstring(char *str1,char *str2)  //checks if string2 is a
substring of string2
{
    char tmp1[30],tmp2[30];
    strcpy(tmp1, str1);
    strcpy(tmp2, str2);
    down_string(tmp1);  //turn it to lowercase
    down_string(tmp2);
    return (strstr(tmp1, tmp2));


}

char** dynamic_string_array(int ROWS, int SIZE)
{
  char **array;
  int i;
  array=(char**) malloc(ROWS*sizeof(char));
  for(i=0;i<ROWS;i++)
     array[i]=(char *) malloc(SIZE*sizeof(char));

  return array;
}


int file_length(FILE *file)
{
  int lines;
  char dummy[100];

  rewind(file);

  lines=0;
  while( fgets(dummy, 100, file) != NULL)
    lines++;

  rewind(file);

  return(lines);
}


Re: Why char** dynamic_string_array(int ROWS, int SIZE) doesn't work properly?

Postby roberson » Tue, 15 Mar 2005 22:25:36 GMT

In article < XXXX@XXXXX.COM >,


:It works fine when I define a 2-D array like char code[ROWS][SIZE].
:But it won't work when I try to define the array dynamically using a
:function. It just crashes.
:Does anyone know why?

:char** dynamic_string_array(int ROWS, int SIZE)
:{
:  char **array;
:  int i;
:  array=(char**) malloc(ROWS*sizeof(char));

You probably want malloc(ROWS*sizeof(char*))
seeing as you are going to be storing in character pointers.

:  for(i=0;i<ROWS;i++)
:     array[i]=(char *) malloc(SIZE*sizeof(char));
:
:  return array;
:}
-- 
Would you buy a used bit from this man??

Re: Why char** dynamic_string_array(int ROWS, int SIZE) doesn't work properly?

Postby David Resnick » Tue, 15 Mar 2005 22:26:12 GMT



SIZE has not been initialized.  Perhaps your compiler could have been so 
kind as to help you with that...

foo.c:12: warning: `SIZE' might be used uninitialized in this function

-David

Re: Why char** dynamic_string_array(int ROWS, int SIZE) doesn't work properly?

Postby Al Bowers » Wed, 16 Mar 2005 01:14:56 GMT


XXXX@XXXXX.COM wrote:

Code snipped.

The code provided here does not compile. The code has numerous
errors. I think it would probably be best to take a step
back and rethink the problem. First, I would decide
on what data structure I need to store the data. Then
write functions that manipulate this data structure.
There are several ways to model the data structure.
Here is an example of one way.

First, make a struct where you can store the data for the class.
typedef struct CLASS
{
char *data;
char *code;
char *course;
char *size;
char *time1;
char *time2;
char *room;
} CLASS;

Then a struct that has as member, an array of class (ex. of the school)
and a member that represents the number of classes.

typedef struct CLASSARR
{
CLASS *class;
size_t cnt;
} CLASSARR;


Then write a function that will add a class and
a function that will print the class array. Test these
functions. Then add other functions that manipulates the data
structure one, or few, at a time, and test them out before
continuing. Then, if you come to a problem that you cannot
solve, the newsgroup can focus and better able to help you.

Example:

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

#define MAXLINE 100

typedef struct CLASS
{
char *data;
char *code;
char *course;
char *size;
char *time1;
char *time2;
char *room;
} CLASS;

typedef struct CLASSARR
{
CLASS *class;
size_t cnt;
} CLASSARR;

int AddCLASSARR(CLASSARR *p, const char *s);
int ParseClassData(CLASS *p, char *data);
void PrintCLASSARR(CLASSARR *p);
void FreeCLASSARR(CLASSARR *p);

int main(void)
{
CLASSARR myschool = {NULL};
FILE *fp;

AddCLASSARR(&myschool,"3511,English,32,8:00am,10:00am,130");
AddCLASSARR(&myschool,"2511,Calculus,21,9:00am,12:00am,141");
puts("\tTest Data");
PrintCLASSARR(&myschool);
FreeCLASSARR(&myschool);
/* Test on a data file */
puts("\n\tData from a file");
if((fp = fopen("test.txt","r")) != NULL)
{
char buf[MAXLINE];
size_t i;
for(i = 0 ; (fgets(buf,MAXLINE,fp)); i++)
if(!AddCLASSARR(&myschool,buf))
printf("Error in File: Line %u\n",i+1);
fclose(fp);
PrintCLASSARR(&myschool);
FreeCLASSARR(&myschool);
}
else puts("Unable to open the file to read data");
return 0;
}

int AddCLASSARR(CLASSARR *p, const char *cs)
{
CLASS *tmp;
char *s;

if((s = malloc(strlen(cs)+1)) == NULL) return 0;
strcpy(s,cs);
tmp = realloc(p->class,(p->cnt+1)*sizeof *tmp);
if(!tmp)
{
free(s);
return 0;
}
p->class = tmp;
if(!ParseClassData(&p->class[p->cnt],s))
{
free(s);
return 0;
}
p->cnt++;
return 1;
}

int ParseClassData(CLASS *p, char *data)
{
int i;
char *s, *tmp[6];

s = strtok(data,",\n");
for(i = 0;(s);i++)
{
if(i < 6) tmp[i] = s;
s = strtok(NULL,",\n");
}
if(i != 6) return 0;
p->code = tmp[0];
p->course = tmp[1];
p->size = tmp[2];
p->time1 = tmp[3];
p->time2 = tmp[4];
p->room = tmp[5];
p->data = data;
return 1;
}

void PrintCLASSARR(CLASSARR *p)
{
size_t i;

for(i = 0; i < p->cnt;i++)
printf("Code: %s\nCourse: %s\nSize: %s\n"

Re: Why char** dynamic_string_array(int ROWS, int SIZE) doesn't work properly?

Postby Keith Thompson » Wed, 16 Mar 2005 03:37:16 GMT

 XXXX@XXXXX.COM  (Walter Roberson) writes:


There's no need to cast the result of malloc() in C.  (Two people in
this newsgroup will disagree with that; one has good reasons to write
code that compiles both as C and as C++, the other is wrong.)

It's also a good idea, in a malloc() call, to use the size of what the
result points to, not the size of a type that can change or that you
can get wrong.

The best way to do the above is:

    array = malloc(ROWS * sizeof *array);

-- 
Keith Thompson (The_Other_Keith)  XXXX@XXXXX.COM   < http://www.**--****.com/ ~kst>
San Diego Supercomputer Center             <*>  < http://www.**--****.com/ ~kst>
We must do something.  This is something.  Therefore, we must do this.

Re: Why char** dynamic_string_array(int ROWS, int SIZE) doesn't work properly?

Postby dddddddd2444444 » Wed, 16 Mar 2005 05:38:55 GMT

Sorry i had sent the wrong version, so it had a few minor errors.
But the problem in question was solved by replacing sizeof(char) with
sizeof(*array) as you adviced.
Thanks.





code[ROWS][SIZE].
a
the
< http://www.**--****.com/ ~kst>
< http://www.**--****.com/ ~kst>
this.


Re: Why char** dynamic_string_array(int ROWS, int SIZE) doesn't work properly?

Postby Old Wolf » Wed, 16 Mar 2005 06:05:34 GMT



What happens when the strings are longer than 29 characters?

You'd be better off to roll your own stristr function that
doesn't need to allocate any memory.


Similar Threads:

1.int main(int argc, char *argv[] ) vs int main(int argc, char **argv )

Is this a style thing?

int main(int argc, char *argv[] )   or   int main(int argc, char **argv )

i.e. *argv[] or  **argv

Why choose the latter?


2.int main(int argc, char *argv[] ) vs int main(int argc, char **argv )

Sidney Cadot < XXXX@XXXXX.COM > wrote in message news:<budvlg$6v7$ XXXX@XXXXX.COM >...
> August Derleth wrote:
> 
> > [...snip...]
>  
> >   int crc32(char *buf, int len);
> > 
> > Would compute the 32-bit cyclical redundancy check of a buffer 
> > containing character values.
> 
> Wouldn't it be more appropriate if the return type was unsigned? The 
> resulting value denotes a polynome with binary coefficients, and there 
> is nothing to justify a special meaning for the sign bit.
> 
> Forthermore, if you use signed ints inside the routine, you are going to 
> do bitwise operations on signed numbers, which may give headaches 
> concerning portability.
> 
> (By pretty much the same token, I'd make the buf's base type an unsigned 
> char.)

I'd shoot for...

  unsigned long crc32(const void *, size_t);

-- 
Peter

3.KILL INT can not wake up $SIG{INT}

Hi list,

Anyone has such experience? KILL INT pid cannot wake up $SIG{INT}  sub{}.

my code is like;

$SIG{INT} = sub {#load pm}

$SIG{ALRM} = sub {print "Driver $pm is going to sleep... bye.\n"; exit;};

the pm actually has a system call to execute something
sub xxx {
`perl xxx.pl` 
}

but  

my $tmp = kill "INT", $pid; ## it returns number of processes, eg: 1
cannot wake up the $SIG{INT}  at all after $SIG{ALRM} has been executed.

does system call cause the problem?

Thanks.

4.[OT] newbie: char* int and char *int

Irrwahn Grausewitz wrote:

> trey < XXXX@XXXXX.COM > wrote:
> <SNIP>
>>
>>[...]I have no idea what I was thinking.  I mean, I did it in the subject
>>line, AND all three declarations.  My only morsel of an excuse is that I
>>was in the middle of watching Law and Order** with my wife while writing
>>it. :)
> <SNIP>
>>** A US tv drama about cops and lawyers - don't want to get flamed for
>>assuming anything about geography, and it appears you are in fact in
>>Germany, at least a .de location using Forte Agent German edition,
>>Standard - the name was also a clue, but I wouldn't want to be
>>name-prejudiced. ;) Of course I could be wrong about all of it :)
> 
> Nope, you are absolutely right, Mr. Holmes :)
> 
> Irrwahn

Ha, well, at least I did something right tonight :). (or morning to you I
believe)

Cheers
trey

5.newbie: char* int and char *int

Hello brilliant people of c.l.c.  -

I have a question about pointer declarations.  Is there a difference between
the following declarations, and if so - what is it ? :)

char* int;
char * int;
char *int;

The type doesn't have to be char particularly.  I'm just wondering if the
placement of the asterick is seriously important.  Seems like it probably
is, but in this little experimental program I just wrote involving
pointers, I tried the first and last declarations and there was no
difference.  Of course, this could have been luck and there is something
undefined going on in the background, which is why I'm posting this ;). 
And since I've seen them used in all three ways, I was curious. 

Thanks for any info...
-trey

6. dirty stuff: f(int,int) cast to f(struct{int,int})

7. Looking for a equivalant function in Perl for main(int argc, char *argv[ ])

8. Hash function for int-aligned text (was: accessing char as int through union)



Return to c

 

Who is online

Users browsing this forum: No registered users and 84 guest