pointer in for loop | Newbie | | Join Date: Dec 2008
Posts: 7
| |
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.... -
for(i=0; i < count; i++)
-
{
-
printf("%d %s %s \n", i,ptr->main[count].key);
-
}It doesnt work.
-
What am I doing wrong in the for loop...
-
-
-
typedef structn ARRAY1{
-
char time[ARRAY_SIZE];
-
char date[ARRAY_SIZE];
-
}ARRAY1,ARR2;
-
-
typedef struct first{
-
char name[10];
-
ARRAY1 *main;
-
}first;
-
-
typedef struct second{
-
char db[10];
-
ARRAY2 *other;
-
}second;
-
in main{
-
FILE *data_txt,
-
struct ARRAY1 *str_ptr;
-
struct ARR2 *new_ptr;
-
str_ptr = &ARRAY1_info;
-
new_ptr = &ARR2_info;
-
-
struct first *ptr;
-
struct second *new;
-
-
data_txt = fopen("data.txt","r");
-
-
while (fgets(line,100,data_txt)!=NULL)
-
{
-
-
-
-
trim(line);
-
strcpy(first_info.name,"ccmlmd");
-
if ((strlen(line) > 1)&&(read!=0)){
-
lcount++;
-
tokens = split(line, delim);
-
for(i = 0; tokens[i] != NULL; i++) {
-
if (i==0){
-
if (tokens[i]!=NULL){
-
ptr->main = malloc(sizeof(detail)*(660)+1);
-
strcpy(ptr->main[count].key,token[i]);
-
printf("key key %d %s ",count, ptr->main[count].key);
-
}
-
}
-
-
}
-
for(i = 0; tokens[i] != NULL; i++)
-
free(tokens[i]);
-
free(tokens);
-
count++;
-
lcount++;
-
}
-
-
// This doesnt work
-
for(i=0; i < count; i++) {
-
printf("%d %s %s", i,ptr->main[i].time, ptr->main[i].date);
-
}
-
|  | Moderator | | Join Date: Dec 2006 Location: Bangalore ,India
Posts: 7,504
| | | re: pointer in for loop
Question moved to C / C++ Forum.
|  | Expert | | Join Date: Mar 2008 Location: California
Posts: 478
| | | 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
| | | re: pointer in for loop
Basically this doesnt print anything -
for(i=0; i < count; i++) {
-
printf("%d %s %s", i,ptr->main[i].time, ptr->main[i].date);
-
}
-
|  | Site Moderator | | Join Date: Jan 2007 Location: America
Posts: 3,387
| | | re: pointer in for loop
Have you tried debugging it? What does printf return to you?
| | Newbie | | Join Date: Dec 2008
Posts: 7
| | | re: pointer in for loop
The printf in the for loop prints only this
But when further up in the code prints just fine. -
ptr->main = malloc(sizeof(detail)*(660)+1);
-
strcpy(ptr->main[count].key,token[i]);
-
printf("key key %d %s ",count, ptr->main[count].key);
-
This prints just fine.
Only problem is that this part of the code doesnt work -
for(i=0; i < count; i++) {
-
printf("%d %s %s", i,ptr->main[i].time, ptr->main[i].date);
-
}
-
|  | Site Moderator | | Join Date: Jan 2007 Location: America
Posts: 3,387
| | | 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
| | | re: pointer in for loop
As I posted above -
typedef structn ARRAY1{
-
char time[ARRAY_SIZE];
-
char date[ARRAY_SIZE];
-
}ARRAY1,ARR2;
-
-
typedef struct first{
-
char name[10];
-
ARRAY1 *main;
-
}first;
-
-
typedef struct second{
-
char db[10];
-
ARRAY2 *other;
-
}second;
-
Thats the info I am trying to catch
|  | Expert | | Join Date: Mar 2008 Location: California
Posts: 478
| | | 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, - 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
| | | 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...? -
for(i=0; i < count; i++) {
-
printf("%d %s %s", i,ptr->main[i].time, ptr->main[i].date);
-
}
-
|  | Expert | | Join Date: Mar 2008 Location: California
Posts: 478
| | | re: pointer in for loop
You do not initialize the date or time members in your code. You do initialize
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
| | | re: pointer in for loop
I am sorry, please subsitite the 'key' and 'value' fields with 'time' and 'date'.
|  | Site Moderator | | Join Date: Jan 2007 Location: America
Posts: 3,387
| | | 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
| | | re: pointer in for loop
OK here is the corrected code. -
typedef structn ARRAY1{
-
char time[ARRAY_SIZE];
-
char date[ARRAY_SIZE];
-
}ARRAY1,ARR2;
-
-
typedef struct first{
-
char name[10];
-
ARRAY1 *main;
-
}first;
-
-
typedef struct second{
-
char db[10];
-
ARRAY2 *other;
-
}second;
-
in main{
-
FILE *data_txt,
-
struct ARRAY1 *str_ptr;
-
struct ARR2 *new_ptr;
-
str_ptr = &ARRAY1_info;
-
new_ptr = &ARR2_info;
-
-
struct first *ptr;
-
struct second *new;
-
-
data_txt = fopen("data.txt","r");
-
-
while (fgets(line,100,data_txt)!=NULL)
-
{
-
-
-
-
trim(line);
-
strcpy(first_info.name,"ccmlmd");
-
if ((strlen(line) > 1)&&(read!=0)){
-
lcount++;
-
tokens = split(line, delim);
-
for(i = 0; tokens[i] != NULL; i++) {
-
if (i==0){
-
if (tokens[i]!=NULL){
-
ptr->main = malloc(sizeof(detail)*(660)+1);
-
strcpy(ptr->main[count].time,token[i]);
-
printf("time time %d %s ",count, ptr->main[count].time);
-
}
-
}
-
-
}
-
for(i = 0; tokens[i] != NULL; i++)
-
free(tokens[i]);
-
free(tokens);
-
count++;
-
lcount++;
-
}
-
-
// This doesnt work
-
for(i=0; i < count; i++) {
-
printf("%d %s ", i,ptr->main[i].time);
-
}
-
| | Needs Regular Fix | | Join Date: Jul 2008
Posts: 380
| | | re: pointer in for loop Quote:
Originally Posted by crumbs12 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...? -
for(i=0; i < count; i++) {
-
printf("%d %s %s", i,ptr->main[i].time, ptr->main[i].date);
-
}
-
If it doesn't print anything, what is value of 'count'?
| | Expert | | Join Date: Mar 2008 Location: Naperville, Illinois U.S.
Posts: 828
| | | 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.
|  | Site Moderator | | Join Date: Jan 2007 Location: America
Posts: 3,387
| | | 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.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|