I'm in the midst of making a C version of a Java program I have developed, and in the same time learning C. I have run into two problems, and while I understand why they are problems, I don't know the solutions.
1. I have an array which I need to move back and forwards in, so I've made a pointer to the array. In Java, I use an iterator and it's next and previous methods to check if I have reach the end of the arraylist. I'm thinking of using sizeof to check if I'm still within the array, but my question is if sizeof(array[0]) would give the same result as sizeof(array[1]). I'm aware of the solution to put a \0 at the end of the array to signify that I have reached the end, and that will be the solution if this doesn't work.
2. I have two functions which have the same name and the same return type, but one has one parameter and calls the other function using a variable specified in the file, and that other function has two parameters. I'm aware of the possibility to use variable parameters in C, but since I only want to have exactly one or two parameters, I would like to avoid this approach.
7 replies to this topic
#1
Posted 28 November 2010 - 04:51 PM
|
|
|
#2
Posted 28 November 2010 - 05:27 PM
1. sizeof operator returnes size of data type in bytes.
2. C does not support function overloading while C++ does. You will have to rename one function.
int a;
printf("%d", sizeof(a)); // on 32-bit systems is 4
Best solution I can think of, have a "global" index counter, by global I mean pass it to those 2 functions and check wheter it's out of range.2. C does not support function overloading while C++ does. You will have to rename one function.
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#3
Posted 28 November 2010 - 09:08 PM
sizeof(array[0]) will return the same thing as sizeof(array[1]). Neither will return the value you are looking for if array is a pointer, and neither will return the size of an array.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.
#4
Posted 29 November 2010 - 05:49 AM
Thanks, I have now changed the name of the function, and I'm using \0 at both ends of the array to know when the array starts and stops. I have however come across one more problem.
I need to concatenate a string for a file name for fopen. In Java I could just + to concatenate strings with variables. I know of strcat, but that takes char, and I have a mixture of strings and unsigned chars. So how can I make this work?
I need to concatenate a string for a file name for fopen. In Java I could just + to concatenate strings with variables. I know of strcat, but that takes char, and I have a mixture of strings and unsigned chars. So how can I make this work?
#5
Posted 29 November 2010 - 06:25 AM
By strings you mean arrays of chars? And why do you need unsigned chars? All numbers and English letters have values less than 128(decimal).
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#6
Posted 29 November 2010 - 06:28 AM
I mean like this:
"Some string" + number + "some other string"
where number is an unsigned char.
"Some string" + number + "some other string"
where number is an unsigned char.
#7
Posted 29 November 2010 - 10:50 AM
Hi,
You can use strncat to concatenate two string to make them one
for example
I hope this helps!
Munir
You can use strncat to concatenate two string to make them one
for example
char *str ="C:\\folder1"; char *str2 = "\\text.txt"; strncat(str, str2, strlen(str2)); //the result would be str = c:\folder1\text.txt
I hope this helps!
Munir
#8
Posted 29 November 2010 - 08:28 PM
use sprintf() to do all the hard work
In c++ programs you can to that with stringstream class instead of sprintf().
char* s1 = "something";
char* s2 = "something else";
int number = 123;
char result[255] = {0};
sprintf(result,"%s%d%s", s1,number,s2);
In c++ programs you can to that with stringstream class instead of sprintf().
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









