Solved

Problem with C pointers

Posted on 2003-10-23
5
213 Views
Last Modified: 2010-04-15
I have a little pointer problem with the attached source code whereas the part staring with - "%02x", *(Family..... - is correct. The output in HtmlPage is unfortunately not correct, i.e. too long and weird so I guess I handled the pointers not properly. The given output contains admittedly the correct output as well at the end of the string.
Can somebody help me?
Best regards,
Mr Bone

char * getSerialNo(SMALLINT deviceNum){
      int x;
      char * temp;
      char * temp1="";
      for(x=7; x>=0; x--){
      sprintf(temp,"%02X", *(FamilySN[deviceNum]+x));
      strcat(temp1,temp);
      }
      return temp1;      
}

sprintf(HtmlPage, getSerialNo(deviceNum));
0
Comment
Question by:LarsBone
  • 3
  • 2
5 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9605320
you are using sprintf and strcat on memory that you have neither allocated nor intialized ... from your code, it seems all you want is concatenated FamilySN[]

#define MAX_STRING_LENGTH 256

char * getSerialNo(SMALLINT deviceNum){
    int x;
    char * temp;
    char * temp1;
    temp = (char * ) malloc ( MAX_STRING_LENGTH );
    temp1=temp;
    for(x=7; x>=0; x--){
    sprintf(temp,"%02X", *(FamilySN[deviceNum]+x));
    temp = temp + strlen(temp);
    }
    return temp1;    
}
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9605322
if you are looking for members of the array to be concatenated
>sprintf(temp,"%02X", *(FamilySN[deviceNum]+x));
should be
sprintf(temp,"%02X", *(FamilySN[deviceNum + x]));
0
 

Author Comment

by:LarsBone
ID: 9605442
Unfortunately that did not work as well. The following code works fine but since I need it very often I want to create a function that returns the string (SerialNo), which is below pasted in the HtmlPage buffer.

for(iii=7; iii>=0; iii--)
{
  sprintf(tmpbuffer,"%02X", *(FamilySN[i]+iii));
  strcat(HtmlPage,tmpbuffer);
}
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 50 total points
ID: 9605451
sprintf(HtmlPage, getSerialNo(deviceNum));
should be

sprintf(HtmlPage, "%s",getSerialNo(deviceNum));
0
 

Author Comment

by:LarsBone
ID: 9605502
Right yeah. And after changing it to as follows it worked fine. THANKS!

char * getSerialNo(SMALLINT deviceNum){
    int x;
    char * temp;
    //char * temp1;
    temp = (char * ) malloc (16);
    //temp1=temp;
    for(x=7; x>=0; x--){
    sprintf(temp,"%02X", *(FamilySN[deviceNum]+x));
    temp = temp + strlen(temp);
    }
    return (temp-16);    
}
...
sprintf(tmpbuffer,"%s",getSerialNo(i));
strcat(HtmlPage,tmpbuffer);
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do I test for current date? 9 95
Can case within switch statement specify range of values ? 3 75
How to learn Linux? 10 43
Why isn't object file created? 6 42
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now