473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

pointer in for loop

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.  
Dec 29 '08 #1
16 3404
debasisdas
8,127 Expert 4TB
Question moved to C / C++ Forum.
Dec 29 '08 #2
boxfish
469 Expert 256MB
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.
Dec 29 '08 #3
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.  
Dec 29 '08 #4
RedSon
5,000 Expert 4TB
Have you tried debugging it? What does printf return to you?
Dec 29 '08 #5
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.  
Dec 29 '08 #6
RedSon
5,000 Expert 4TB
what type of data is ptr->main[i].time and ptr->main[i].date?
Dec 29 '08 #7
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
Dec 29 '08 #8
boxfish
469 Expert 256MB
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.
Dec 29 '08 #9
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.  
Dec 30 '08 #10
boxfish
469 Expert 256MB
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.
Dec 30 '08 #11
I am sorry, please subsitite the 'key' and 'value' fields with 'time' and 'date'.
Dec 30 '08 #12
RedSon
5,000 Expert 4TB
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.
Dec 30 '08 #13
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.  
Dec 30 '08 #14
newb16
687 512MB
@crumbs12
If it doesn't print anything, what is value of 'count'?
Dec 30 '08 #15
donbock
2,426 Expert 2GB
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.
Dec 31 '08 #16
RedSon
5,000 Expert 4TB
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.
Dec 31 '08 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: lawrence | last post by:
I had some code that worked fine for several weeks, and then yesterday it stopped working. I'm not sure what I did. Nor can I make out why it isn't working. I'm running a query that should return 3...
5
by: lawrence | last post by:
I posted before, but have now narrowed my problem down to this method. At the start of the method, I test to make sure that I have a resource, a pointer to data returned from a database. This test...
22
by: lokman | last post by:
Hi, In the following code, can someone tell me the difference between *p++ and p++ ? I can see both achieve the same result. Thanks a lot !
7
by: Fatted | last post by:
I'm trying to learn how to create arrays dynamically. But its just not happening. Have a look at code below and point and laugh where appropriate... First part of program, I'm using an array of...
26
by: Meenu | last post by:
Hi, Can two different far pointers contain two different addresses but refer to the same location in memory?
26
by: Martin Jørgensen | last post by:
Hi, I don't understand these errors I get: g++ Persort.cpp Persort.cpp: In function 'int main()': Persort.cpp:43: error: name lookup of 'j' changed for new ISO 'for' scoping Persort.cpp:37:...
18
by: Jacek Dziedzic | last post by:
Hi! I'm trying to squeeze a few clock cycles from a tight loop that profiling shows to be a bottleneck in my program. I'm at a point where the only thing that matters is execution speed, not...
4
by: code break | last post by:
Hi all, What is the difference between stack pointer and frame pointer ? Any suggestions are welcome ,,,
4
by: Bernd Gaertner | last post by:
Dear experts, according to my interpretation of the Standard, loop 1 in the following program is legal, while loop 2 is not (see explanation below). This looks a bit counterintuitive, though; do...
9
by: shaun roe | last post by:
Question about pointer-to-data members I have a deeply nested loop, the innermost bit looks a little like this: for(it(vec.begin(), end(vec.end()), it!=end;++it){ if (global == 0){ myObj =...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.