Connecting Tech Pros Worldwide Help | Site Map

pointer in for loop

Newbie
 
Join Date: Dec 2008
Posts: 7
#1: Dec 29 '08
hello friends,
I am having a problem.

This code works just fine when I print as soon as I assign the string to
the pointer.

Code:

ptr->main = malloc(sizeof(detail)*(660)+1);
strcpy(ptr->main[count].key,token[i]);
printf("key key %d %s ",count, ptr->main[count].key);But when I try to do this....

Expand|Select|Wrap|Line Numbers
  1.       for(i=0; i < count; i++)
  2.    {
  3.           printf("%d %s %s \n", i,ptr->main[count].key);
  4.       }It doesnt work.
  5. What am I doing wrong in the for loop...
  6.  
  7.  
Expand|Select|Wrap|Line Numbers
  1. typedef structn ARRAY1{
  2.         char time[ARRAY_SIZE];
  3.         char date[ARRAY_SIZE];
  4. }ARRAY1,ARR2;
  5.  
  6. typedef struct first{
  7.       char name[10];
  8.        ARRAY1 *main;
  9.    }first;
  10.  
  11. typedef struct second{
  12.       char db[10];
  13.         ARRAY2 *other;
  14.     }second;
  15. in main{
  16.   FILE *data_txt,
  17.         struct ARRAY1 *str_ptr;   
  18.         struct ARR2 *new_ptr;
  19.       str_ptr = &ARRAY1_info;
  20.       new_ptr = &ARR2_info;
  21.  
  22.        struct first *ptr;
  23.        struct second *new;
  24.  
  25.      data_txt = fopen("data.txt","r");
  26.  
  27.  while (fgets(line,100,data_txt)!=NULL)
  28.  {
  29.  
  30.  
  31.  
  32. trim(line);
  33.  strcpy(first_info.name,"ccmlmd");
  34.         if ((strlen(line) > 1)&&(read!=0)){
  35.         lcount++;
  36.         tokens = split(line, delim);
  37.         for(i = 0; tokens[i] != NULL; i++) {
  38.         if (i==0){
  39.         if (tokens[i]!=NULL){
  40. ptr->main = malloc(sizeof(detail)*(660)+1);
  41.         strcpy(ptr->main[count].key,token[i]);
  42.       printf("key key %d %s ",count, ptr->main[count].key);
  43.        }
  44.       }
  45.  
  46.    }
  47.   for(i = 0; tokens[i] != NULL; i++)
  48.   free(tokens[i]);
  49.   free(tokens);
  50.   count++;
  51.   lcount++;
  52.       }
  53.  
  54. // This doesnt work
  55.       for(i=0; i < count; i++) {
  56.  printf("%d %s %s", i,ptr->main[i].time, ptr->main[i].date);
  57.       }
  58.  
debasisdas's Avatar
Moderator
 
Join Date: Dec 2006
Location: Bangalore ,India
Posts: 7,504
#2: Dec 29 '08

re: pointer in for loop


Question moved to C / C++ Forum.
boxfish's Avatar
Expert
 
Join Date: Mar 2008
Location: California
Posts: 478
#3: Dec 29 '08

re: pointer in for loop


In what way does it not work? If it gives you compiler errors, please post them here. If it gives you the wrong output, please post it here, along with the expected output. This will make it much easier for people to help you. Thanks.
Newbie
 
Join Date: Dec 2008
Posts: 7
#4: Dec 29 '08

re: pointer in for loop


Basically this doesnt print anything
Expand|Select|Wrap|Line Numbers
  1.  for(i=0; i < count; i++) { 
  2.  printf("%d %s %s", i,ptr->main[i].time, ptr->main[i].date); 
  3.       }
  4.  
RedSon's Avatar
Site Moderator
 
Join Date: Jan 2007
Location: America
Posts: 3,387
#5: Dec 29 '08

re: pointer in for loop


Have you tried debugging it? What does printf return to you?
Newbie
 
Join Date: Dec 2008
Posts: 7
#6: Dec 29 '08

re: pointer in for loop


The printf in the for loop prints only this
Expand|Select|Wrap|Line Numbers
  1.  
  2. 1   
  3. 2   
  4.  
  5.  
But when further up in the code prints just fine.
Expand|Select|Wrap|Line Numbers
  1. ptr->main = malloc(sizeof(detail)*(660)+1); 
  2.         strcpy(ptr->main[count].key,token[i]); 
  3.       printf("key key %d %s ",count, ptr->main[count].key);
  4.  
This prints just fine.

Only problem is that this part of the code doesnt work
Expand|Select|Wrap|Line Numbers
  1.       for(i=0; i < count; i++) { 
  2.  printf("%d %s %s", i,ptr->main[i].time, ptr->main[i].date); 
  3.       } 
  4.  
RedSon's Avatar
Site Moderator
 
Join Date: Jan 2007
Location: America
Posts: 3,387
#7: Dec 29 '08

re: pointer in for loop


what type of data is ptr->main[i].time and ptr->main[i].date?
Newbie
 
Join Date: Dec 2008
Posts: 7
#8: Dec 29 '08

re: pointer in for loop


As I posted above
Expand|Select|Wrap|Line Numbers
  1. typedef structn ARRAY1{ 
  2.         char time[ARRAY_SIZE]; 
  3.         char date[ARRAY_SIZE]; 
  4. }ARRAY1,ARR2; 
  5.  
  6. typedef struct first{ 
  7.       char name[10]; 
  8.        ARRAY1 *main; 
  9.    }first; 
  10.  
  11. typedef struct second{ 
  12.       char db[10]; 
  13.         ARRAY2 *other; 
  14.     }second; 
  15.  
Thats the info I am trying to catch
boxfish's Avatar
Expert
 
Join Date: Mar 2008
Location: California
Posts: 478
#9: Dec 29 '08

re: pointer in for loop


It's hard for me to tell what's wrong with your program because the code you posted has typos and does not compile. Two problems I can see are that in your posted code, you have not initialized ptr->main[i].time or ptr->main[i].date, and the line you posted separately,
Expand|Select|Wrap|Line Numbers
  1. printf("%d %s %s \n", i,ptr->main[count].key);
has one too many %s, but I don't know if these things are causing problems for you.
Newbie
 
Join Date: Dec 2008
Posts: 7
#10: Dec 30 '08

re: pointer in for loop


Oh ok...But since I have already initialized the pointer further up...wouldnt that be enough...or do I have to initialize the ptr again in this part of the code...?
Expand|Select|Wrap|Line Numbers
  1.   for(i=0; i < count; i++) {  
  2.  printf("%d %s %s", i,ptr->main[i].time, ptr->main[i].date);  
  3.       } 
  4.  
boxfish's Avatar
Expert
 
Join Date: Mar 2008
Location: California
Posts: 478
#11: Dec 30 '08

re: pointer in for loop


You do not initialize the date or time members in your code. You do initialize
Expand|Select|Wrap|Line Numbers
  1. ptr->main[count].key
but the ARRAY1 struct does not have a member called key. I'm afraid the code you posted does not make any sense.
Newbie
 
Join Date: Dec 2008
Posts: 7
#12: Dec 30 '08

re: pointer in for loop


I am sorry, please subsitite the 'key' and 'value' fields with 'time' and 'date'.
RedSon's Avatar
Site Moderator
 
Join Date: Jan 2007
Location: America
Posts: 3,387
#13: Dec 30 '08

re: pointer in for loop


Instead of making us substitute everything and try to force your code to work, why don't you provide a listing of your code that compiles so we can see what you are doing.
Newbie
 
Join Date: Dec 2008
Posts: 7
#14: Dec 30 '08

re: pointer in for loop


OK here is the corrected code.

Expand|Select|Wrap|Line Numbers
  1. typedef structn ARRAY1{
  2.         char time[ARRAY_SIZE];
  3.         char date[ARRAY_SIZE];
  4. }ARRAY1,ARR2;
  5.  
  6. typedef struct first{
  7.       char name[10];
  8.        ARRAY1 *main;
  9.    }first;
  10.  
  11. typedef struct second{
  12.       char db[10];
  13.         ARRAY2 *other;
  14.     }second;
  15. in main{
  16.   FILE *data_txt,
  17.         struct ARRAY1 *str_ptr;   
  18.         struct ARR2 *new_ptr;
  19.       str_ptr = &ARRAY1_info;
  20.       new_ptr = &ARR2_info;
  21.  
  22.        struct first *ptr;
  23.        struct second *new;
  24.  
  25.      data_txt = fopen("data.txt","r");
  26.  
  27.  while (fgets(line,100,data_txt)!=NULL)
  28.  {
  29.  
  30.  
  31.  
  32. trim(line);
  33.  strcpy(first_info.name,"ccmlmd");
  34.         if ((strlen(line) > 1)&&(read!=0)){
  35.         lcount++;
  36.         tokens = split(line, delim);
  37.         for(i = 0; tokens[i] != NULL; i++) {
  38.         if (i==0){
  39.         if (tokens[i]!=NULL){
  40. ptr->main = malloc(sizeof(detail)*(660)+1);
  41.         strcpy(ptr->main[count].time,token[i]);
  42.       printf("time time %d %s ",count, ptr->main[count].time);
  43.        }
  44.       }
  45.  
  46.    }
  47.   for(i = 0; tokens[i] != NULL; i++)
  48.   free(tokens[i]);
  49.   free(tokens);
  50.   count++;
  51.   lcount++;
  52.       }
  53.  
  54. // This doesnt work
  55.       for(i=0; i < count; i++) {
  56.  printf("%d %s ", i,ptr->main[i].time);
  57.       }
  58.  
Needs Regular Fix
 
Join Date: Jul 2008
Posts: 380
#15: Dec 30 '08

re: pointer in for loop


Quote:

Originally Posted by crumbs12 View Post

Oh ok...But since I have already initialized the pointer further up...wouldnt that be enough...or do I have to initialize the ptr again in this part of the code...?

Expand|Select|Wrap|Line Numbers
  1.   for(i=0; i < count; i++) {  
  2.  printf("%d %s %s", i,ptr->main[i].time, ptr->main[i].date);  
  3.       } 
  4.  

If it doesn't print anything, what is value of 'count'?
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 828
#16: Dec 31 '08

re: pointer in for loop


The code in post #14 is incomplete and contains typos ...

Line 1: "struct", not "structn".
Lines 2 and 3: ARRAY_SIZE is undefined.
Line 16: "int", not "in".
Line 17: this implies that you included <stdio.h>.
Lines 20 and 21: ARRAY1_info and ARR2_info are undefined.
Line 26: you don't check if fopen fails.
Line 28: "line" is undefined.
Line 33: function "trim" is undefined.
Line 34: this implies that you included <string.h>.
Line 34: "first_info" is undefined.
Line 35: "read" is undefined and uninitialized.
Line 36: "lcount" is undefined and uninitialized.
Line 37: "tokens" is undefined.
Line 37: function "split" is undefined.
Line 37: "delim" is undefined and uninitialized.
Line 38: "i" is undefined.
Line 41: "ptr" is uninitialized.
Line 41: "detail" is undefined.
Line 41: this implies that you included <stdlib.h>.
Line 41: you don't check if malloc fails.
Line 42: "count" is undefined and uninitialized.
Line 42: "tokens", not "token" ... I think.
Lines 49 and 50: you didn't malloc either of these, but you're freeing them. I suppose trim() must have malloc'd them.
Line 57: ptr->main was malloc'd at line 41, are you sure it wasn't also free'd by lines 49 or 50?


These aren't errors, but could be cause for confusion:

Lines 1 and 4; 6 and 9; 11 and 14: the struct name is the same as the typedef name. Some people prefer to do it like this, others prefer to avoid all ambiguity.
Line 8: "main" is typically pseudo-reserved for the name of the main function.
Lines 18, 19, 23, and 24: after going to the trouble of using typedefs you ended up using the structure declarations.

These comments are based on code inspection. Additional problems might come to light if I tried to compile the code.
RedSon's Avatar
Site Moderator
 
Join Date: Jan 2007
Location: America
Posts: 3,387
#17: Dec 31 '08

re: pointer in for loop


If you don't want to copy your code entirely to the site use a service like Free File Hosting Made Simple - MediaFire to upload a zipped copy of your code for others on this site to inspect. Make sure you link to it properly here.
Reply