char* -> char (newbie question)
Author |
Message |
Randall Skelto #1 / 15
|
 char* -> char (newbie question)
OK. I have a 'simple' C problem which I cannot figure out. I have a function which returns a pointer (char *) which I call repeatedly from i to i_total. I want to fill an array of strings with the output but I am getting lost on how to do it. Can anyone send me some help? How should I define the array of strings (i.e. it is a multidimensional array) and should I use pointers or not. Then, how do I get each element of this array to contain the value returned by my function (I think I need strcpy here because I am reseting the function and will loose the data if I don't make a copy). At the moment I have: char temparray[100][20]; /* D2 array 100x20 */ ... for (i=0; i<PQntuples(res); i++) { strcpy(temp_array[i][20], PQgetvalue(res, i, 0)); printf("%s",temp_array[i][20]); } where PQgetvalue(res,i,0) returns different values (char*) for each i. The printf statement segfaults and I get a warning on compile that I am "passing arg 1 of `strcpy' makes pointer from integer without a cast". I have tried adding (char*) or (char) as a cast but I just get garbage... Obviously I am missing something here. Thanks in advance for the help, Randall --
|
Sat, 03 May 2003 03:00:00 GMT |
|
 |
Morris Dove #2 / 15
|
 char* -> char (newbie question)
Quote:
> OK. I have a 'simple' C problem which I cannot figure out. > I have a function which returns a pointer (char *) which I call repeatedly > from i to i_total. I want to fill an array of strings with the > output but I am getting lost on how to do it. Can anyone send me some > help? > How should I define the array of strings (i.e. it is a multidimensional > array) and should I use pointers or not. Then, how do I get each element > of this array to contain the value returned by my function (I think I need > strcpy here because I am reseting the function and will loose the data if > I don't make a copy). > At the moment I have: > char temparray[100][20]; /* D2 array 100x20 */ > ... > for (i=0; i<PQntuples(res); i++) > { > strcpy(temp_array[i][20], PQgetvalue(res, i, 0)); > printf("%s",temp_array[i][20]); > } > where PQgetvalue(res,i,0) returns different values (char*) for each > i. The printf statement segfaults and I get a warning on compile > that I am "passing arg 1 of `strcpy' makes pointer from integer without a > cast". I have tried adding (char*) or (char) as a cast but I just get > garbage... Obviously I am missing something here.
Randall... How about: for (i=0; i<PQntuples(res); i++) { strcpy(temp_array[i][0], PQgetvalue(res, i, 0)); printf("%s",temp_array[i][0]); } Can you see why? -- Morris Dovey West Des Moines, Iowa USA
--
|
Sun, 04 May 2003 03:00:00 GMT |
|
 |
-hs- #3 / 15
|
 char* -> char (newbie question)
Randall Skelton a crit dans le message ... Quote: >OK. I have a 'simple' C problem which I cannot figure out. >I have a function which returns a pointer (char *) which I call repeatedly >from i to i_total. I want to fill an array of strings with the >output but I am getting lost on how to do it. Can anyone send me some >help?
I'll try. Quote: >How should I define the array of strings (i.e. it is a multidimensional >array) and should I use pointers or not. Then, how do I get each element >of this array to contain the value returned by my function (I think I need >strcpy here because I am reseting the function and will loose the data if >I don't make a copy).
Ok, so you need an array of strings. Quote: >At the moment I have: >char temparray[100][20]; /* D2 array 100x20 */
100 strings of 19 char + a trailing 0. Quote: >... >for (i=0; i<PQntuples(res); i++)
It is rarely a good idea to have a function call here. Better to call the function once before the loop starts, and store the result, { int n = PQntuples(res); for (i = 0; i < n; i++) { } } unless you intentionally want a run-time variable length loop. In that case, beware not to fall into an infinite loop. Quote: > { > strcpy(temp_array[i][20], PQgetvalue(res, i, 0));
Are you sure that the address returned by PQgetvalue is still valid ? Is there a posibility that it is was the address of an automatic data ? Are you sure that pointed string was terminated by a 0? I would be very interested to have a look to the code of this function. I am quite sure that the bug comes from there. Quote: > printf("%s",temp_array[i][20]);
That's a char, not a string. You want printf("%s\n",temp_array[i]); This can also explain the bug. And a trailing '\n' was missing. Quote: > } >where PQgetvalue(res,i,0) returns different values (char*) for each >i. The printf statement segfaults and I get a warning on compile >that I am "passing arg 1 of `strcpy' makes pointer from integer without a >cast". I have tried adding (char*) or (char) as a cast but I just get >garbage... Obviously I am missing something here.
Yes. "%s" expects a string (a char*), not a char. According to your definition: char temparray[100][20]; /* D2 array 100x20 */ temp_array[i][20] is an undefined char (they are defined from 0 to 19) temp_array[i] is a string, assuming 'i' is between 0 and 99 inclusive, and that the sequence of char is terminated by a 0 in bounds (0-19). -- -hs- Tabs out, spaces in. CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html ISO-C Library: http://www.dinkum.com/htm_cl FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc --
|
Sun, 04 May 2003 03:00:00 GMT |
|
 |
Will Brigg #4 / 15
|
 char* -> char (newbie question)
Quote: > OK. I have a 'simple' C problem which I cannot figure out. > I have a function which returns a pointer (char *) which I call repeatedly > from i to i_total. I want to fill an array of strings with the > output but I am getting lost on how to do it. Can anyone send me some > help? > How should I define the array of strings (i.e. it is a multidimensional > array) and should I use pointers or not. Then, how do I get each element > of this array to contain the value returned by my function (I think I need > strcpy here because I am reseting the function and will loose the data if > I don't make a copy). > At the moment I have: > char temparray[100][20]; /* D2 array 100x20 */ > ... > for (i=0; i<PQntuples(res); i++) > { > strcpy(temp_array[i][20], PQgetvalue(res, i, 0)); > printf("%s",temp_array[i][20]); > }
This problem itself is elementary; however, the concepts you need to know in order to solve it are tricky when you are first learning the language. At the moment, your problem is in the strcpy line. You are attempting to copy a pointer-to-char into a character; strcpy should take the form int strcpy(char *, const char *). If I understand your problem correctly, you want to fill the entire two-dimensional character array with values returned by this function. In that case, you would do something like this: for(i = 0; i < 100; i++) for(j = 0; j < 20; j++) temp_array[i][j] = *(PQgetvalue(res, i 0)); Good luck, and let me know if this is actually what you are looking for. -Will Quote: > where PQgetvalue(res,i,0) returns different values (char*) for each > i. The printf statement segfaults and I get a warning on compile > that I am "passing arg 1 of `strcpy' makes pointer from integer without a > cast". I have tried adding (char*) or (char) as a cast but I just get > garbage... Obviously I am missing something here. > Thanks in advance for the help, > Randall > --
--
|
Sun, 04 May 2003 03:00:00 GMT |
|
 |
Robert Stankowi #5 / 15
|
 char* -> char (newbie question)
Randall Skelton schrieb: Quote: > OK. I have a 'simple' C problem which I cannot figure out. > I have a function which returns a pointer (char *) which I call repeatedly > from i to i_total. I want to fill an array of strings with the > output but I am getting lost on how to do it. Can anyone send me some > help? > How should I define the array of strings (i.e. it is a multidimensional > array) and should I use pointers or not. Then, how do I get each element > of this array to contain the value returned by my function (I think I need > strcpy here because I am reseting the function and will loose the data if > I don't make a copy). > At the moment I have: > char temparray[100][20]; /* D2 array 100x20 */ > ... > for (i=0; i<PQntuples(res); i++) > { > strcpy(temp_array[i][20], PQgetvalue(res, i, 0)); > printf("%s",temp_array[i][20]);
Randall, your printf() is incorrect, it should be printf("%s",temp_array[i]); (maybe with "%s\n" if you want to show every string in a separate line. Or use fflush(stdout) after the printf() if you don't wnt a newline. HTH Robert --
I don't make my mistakes more than once. I store them carefully and after some time I take them out again, add some new features and _reuse_ them. --
|
Sun, 04 May 2003 03:00:00 GMT |
|
 |
Barry Schwar #6 / 15
|
 char* -> char (newbie question)
Quote: >OK. I have a 'simple' C problem which I cannot figure out. >I have a function which returns a pointer (char *) which I call repeatedly >from i to i_total. I want to fill an array of strings with the >output but I am getting lost on how to do it. Can anyone send me some >help? >How should I define the array of strings (i.e. it is a multidimensional >array) and should I use pointers or not. Then, how do I get each element >of this array to contain the value returned by my function (I think I need >strcpy here because I am reseting the function and will loose the data if >I don't make a copy). >At the moment I have: >char temparray[100][20]; /* D2 array 100x20 */ >... >for (i=0; i<PQntuples(res); i++) > { > strcpy(temp_array[i][20], PQgetvalue(res, i, 0)); > printf("%s",temp_array[i][20]); > } >where PQgetvalue(res,i,0) returns different values (char*) for each >i. The printf statement segfaults and I get a warning on compile >that I am "passing arg 1 of `strcpy' makes pointer from integer without a >cast". I have tried adding (char*) or (char) as a cast but I just get >garbage... Obviously I am missing something here. >Thanks in advance for the help, >Randall
Ignoring the misspelling in the declaration, temp_array[i][20] is a non-existent element of array temp_array. The first array subscript must be in the range of 0 to 99 and the second in the range of 0 to 19. Back to basics: char temp_array[100][20]; declares temp_array to be an array of 100 arrays of 20 chars. In your case, you are using this to mean an array of 100 strings, each with an allocated length of 20 chars which allows a max string length of 19 (to allow room for the mandatory '\0'). If you want to copy into or print from the i-th string, the correct designation is temp_array[i]. This is the name of the i-th array of 20 chars. <<Remove the del for email>> --
|
Sun, 04 May 2003 03:00:00 GMT |
|
 |
Michiel Salter #7 / 15
|
 char* -> char (newbie question)
Quote:
> OK. I have a 'simple' C problem which I cannot figure out.
Stings and 2D arrays in C are indeed 'simple', not simple. Quote: > I have a function which returns a pointer (char *) which I call repeatedly > from i to i_total. I want to fill an array of strings with the > output but I am getting lost on how to do it. Can anyone send me some > help?
Send, not really. Post, yes. We're not solving individual problems here as much as helping people understand C - but your problem makes a good example. Quote: > How should I define the array of strings (i.e. it is a multidimensional > array) and should I use pointers or not. Then, how do I get each element > of this array to contain the value returned by my function (I think I need > strcpy here because I am reseting the function and will loose the data if > I don't make a copy).
Using pointers can't be avoided in this problem, but the question where they should be used is valid. For starters, you get a pointer to a char array (string) back. The documentation of the function should say what you should do with it. It might very well be a pointer to an internal buffer, in which case the documentation will say that you need to copy the string. strcpy() will do that for you. Quote: > At the moment I have: > char temparray[100][20]; /* D2 array 100x20 */ > ... > for (i=0; i<PQntuples(res); i++) > { > strcpy(temp_array[i][20], PQgetvalue(res, i, 0)); > printf("%s",temp_array[i][20]); > } > where PQgetvalue(res,i,0) returns different values (char*) for each > i. The printf statement segfaults and I get a warning on compile > that I am "passing arg 1 of `strcpy' makes pointer from integer without a > cast". I have tried adding (char*) or (char) as a cast but I just get > garbage... Obviously I am missing something here.
Well, there are some questions that you should have asked yourself. Will the string returned fit in 19+1 characters? If so, your array is big enough. Next question, what does strcpy need? It needs a pointer to the first char of the destination, and a pointer to the first char of the source. The source is ok, but the destination? There is no character temp_array[i][20], only temp_array[i][0] to temp_array[i][19], and the last one should be reserved for a '\0' terminator. The first character of the 'i'th string is of course temp_array[i][0], and & will get its address. printf() has the same problem. Casting will force garbage (i.e. temp_array[i][20]) to be interpreted as a pointer. You should again pass a real pointer to the first char in the array. The problem occurs at runtime, because printf() can accept many arguments, and temp_array[i][20] could be a valid argument. That is, if temp_array was bigger and the format specifier was %c, in which case the 21st character would be printed. Quote: > Randall
HTH, -- Michiel Salters
--
|
Sun, 04 May 2003 03:00:00 GMT |
|
 |
LucaD #8 / 15
|
 char* -> char (newbie question)
Quote:
>for (i=0; i<PQntuples(res); i++) > { > strcpy(temp_array[i][20], PQgetvalue(res, i, 0)); > printf("%s",temp_array[i][20]); > }
You have to pass a char* to strcpy and printf. Try for (i=0; i<PQntuples(res); i++) { strcpy(temp_array[i] /* now it's a char* */ , PQgetvalue(res, i, 0)); printf("%s",temp_array[i]); } bye Luca --
|
Sun, 04 May 2003 03:00:00 GMT |
|
 |
David Rubi #9 / 15
|
 char* -> char (newbie question)
Quote:
> char temparray[100][20]; /* D2 array 100x20 */
The comment here is a useless. Also, I assume you meant to write 'temp_array' here. Quote: > for (i=0; i<PQntuples(res); i++) > { > strcpy(temp_array[i][20], PQgetvalue(res, i, 0));
temp_array[i][20] is of type char, not char*, so 1. this is wrong, and 2. your compiler should have told you so. Try calling strcpy(temp_array[i],PQgetvalue(res,i,0)); However, IMO it's bettern to use strncpy which prevents overflow. Quote: > printf("%s",temp_array[i][20]);
Same problem; try temp_array[i] instead. What is at work here is the equivalence of the value at temp_array[i] and the address temp_array[i][0] (first element of the array at i). In short, a variable whose value is an address is a pointer: temp_array[i] = &(temp_array[i][0]) (parentheses for emphasis), and char *p = temp_array[i] implies p==temp_array[i]==&(temp_array[i][0]) Quote: > } > where PQgetvalue(res,i,0) returns different values (char*) for each > i. The printf statement segfaults and I get a warning on compile > that I am "passing arg 1 of `strcpy' makes pointer from integer without a > cast". I have tried adding (char*) or (char) as a cast but I just get > garbage... Obviously I am missing something here.
david -- fortran was the language of choice for the same reason that three-legged races are popular. -- Ken Thompson, "Reflections on Trusting Trust" --
|
Sun, 04 May 2003 03:00:00 GMT |
|
 |
Mike Dowli #10 / 15
|
 char* -> char (newbie question)
Quote: >OK. I have a 'simple' C problem which I cannot figure out. >I have a function which returns a pointer (char *) which I call repeatedly >from i to i_total. I want to fill an array of strings with the >output but I am getting lost on how to do it. Can anyone send me some >help? >At the moment I have: >char temparray[100][20]; /* D2 array 100x20 */ >... >for (i=0; i<PQntuples(res); i++)
Can you guarantee that PQntuples never returns a value greater than 100? Otherwise you will get segmentation faults. Quote: > { > strcpy(temp_array[i][20], PQgetvalue(res, i, 0));
Here you are copying a single char (i.e., an integer type) using strcpy, which expects a string, i.e., char *. Can you guarantee that PQgetvalue always returns a string with no more that 20 characters including the \0? If not, you will get segmentation faults once you have corrected the problem. Quote: > printf("%s",temp_array[i][20]);
Again, printf expects char *, but receives char. If you know in advance how many strings you have and also know how long they are, you can work with statically allocated arrays as you have done here. Otherwise, you should allocate space dynamically using malloc. Quote: >How should I define the array of strings (i.e. it is a multidimensional >array) and should I use pointers or not.
You could declare it as char **. Quote: >should I use pointers or not.
You don't have to use pointers; it could make your code more legible if you don't. You could have strcpy(temp_array[i], PQgetvalue(res, i, 0)); Quote: >Then, how do I get each element of this array to contain the value >returned by my function
Do you want each element or each string? temp_array[i][j] is the jth char in the ith string. Cheers, Mike --
address. It is a mail alias. Once spammed, the alias is deleted, and the integer 'N' incremented. Currently, mike[35,36] are valid. If email to mikeN bounces, try mikeN+1. --
|
Sun, 04 May 2003 03:00:00 GMT |
|
 |
Richard B #11 / 15
|
 char* -> char (newbie question)
Quote:
> I have a function which returns a pointer (char *) which I call repeatedly > from i to i_total. I want to fill an array of strings with the > output but I am getting lost on how to do it. Can anyone send me some > help? > How should I define the array of strings (i.e. it is a multidimensional > array) and should I use pointers or not. Then, how do I get each element > of this array to contain the value returned by my function (I think I need > strcpy here because I am reseting the function and will loose the data if > I don't make a copy). > At the moment I have: > char temparray[100][20]; /* D2 array 100x20 */
Be careful. Your size requirements may change. It may not be wise to use a fixed size array; it's very probably unwise to use direct constants for this; even with fixed size arrays, a #define is much easier to work with and get right. Also, you've spelled this temp_array below; I'll assume you meant the same identifier in all cases. Quote: > for (i=0; i<PQntuples(res); i++) > { > strcpy(temp_array[i][20], PQgetvalue(res, i, 0)); > printf("%s",temp_array[i][20]);
temp_array is an array of arrays of char. Therefore, temp_array[i] is an array of char, and temp_array[i][20] is a char. But strcpy takes a char * (or, because arrays decay to pointers when passed to a function, a char []), so this is not correct. What you, presumably, intended to do was to copy PQgetvalue(res,i,0) into the i-th character string. In that case, what you should do is use temp_array[i], because temp_array[i] is the i-th string, but temp_array[i][20] is the 20th character of the i-th string. Ditto, of course, for the printf(). Note also that you'd better be very sure that PQgetvalue can't be longer than 19 characters plus one terminating null char, because if it is you'll write outside the edges of your array, invooking undefined behaviour. Richard --
|
Sun, 04 May 2003 03:00:00 GMT |
|
 |
Nick Selwy #12 / 15
|
 char* -> char (newbie question)
Follow these definitions: temparray is an array of 100 arrays of 20 chars. temparray[i] is a pointer to one of those sub-arrays, and can be regarded as a pointer-to-char. (See where this is going?) As your compiler is telling you, something is amiss in the strcpy call. You are in fact passing it a char (a specialized form of integer, for this purpose), rather than what it requires, which is a char*. There is a similar problem in the call to printf, which, when given a %s in the formatting string, requires a char* also. The actual code modifications are left as an exercise. Quote:
> <snip> > At the moment I have: > char temparray[100][20]; /* D2 array 100x20 */ > ... > for (i=0; i<PQntuples(res); i++) > { > strcpy(temp_array[i][20], PQgetvalue(res, i, 0)); > printf("%s",temp_array[i][20]); > } > where PQgetvalue(res,i,0) returns different values (char*) for each > i. The printf statement segfaults and I get a warning on compile > that I am "passing arg 1 of `strcpy' makes pointer from integer without a > cast". I have tried adding (char*) or (char) as a cast but I just get > garbage... Obviously I am missing something here. > Thanks in advance for the help, > Randall > --
-- Nick Selwyn ********************************************************************* * Any comments or statements made are not necessarily those of * * Fidelity Investments, its subsidiaries, or affiliates. * *********************************************************************
|
Sun, 04 May 2003 03:00:00 GMT |
|
 |
Ken Sodema #13 / 15
|
 char* -> char (newbie question)
Quote: >How should I define the array of strings (i.e. it is a multidimensional >array) and should I use pointers or not. Then, how do I get each element >of this array to contain the value returned by my function (I think I need >strcpy here because I am reseting the function and will loose the data if >I don't make a copy).
correct. Quote: >At the moment I have: >char temparray[100][20]; /* D2 array 100x20 */ >... >for (i=0; i<PQntuples(res); i++)
PQntuples returns the number of tuples in the result set. How can you be sure that the result set is less than 100 tuples? Since the result set is the result of a database query (I am guess from the function names that you are using PostgreSQL and libpq), the size of the result set will grow as more data is entered into the database. You either need a more dynamic solution, or you need to limit the value returned by PQntuples() before using it in a loop. For example: n = PQntuples (res); n = (n > 100) ? 100 : n; for (i = 0; i < n; i++) { ... } This limits i to be within the size of your structure, and it remove the call to PQntuples from the loop. A more dynamic solution (building a linked list of the tuples, for example) is probaby better. However, I don't know what your needs are, nor your skill level. Obviously, if this is an assignment for school or something of that nature, you are constrained by the requirements of the assignment. However, if this is for "real world" code or even just "learning on your own" code, I would seriously look at a more dynamic solution.... Also, how are you keeping track of the number of tuples currently stored? Quote: > { > strcpy(temp_array[i][20], PQgetvalue(res, i, 0));
This line should be something like: strcpy (temp_array[i], PQgetvalue(res, i, 0)); 'temp_array[i]' is the ith string. OTOH, 'temp_array[i][20] is the 20th character of the ith string. Quote: > printf("%s",temp_array[i][20]);
Similar comment here. arg 2 is not a string as you have it coded, it is simply a character. temp_array[i] is the string you are looking for. Quote: > }
-- Ken Sodemann
http://www.execpc.com/~stuffle --
|
Sun, 04 May 2003 03:00:00 GMT |
|
 |
Francis Glassboro #14 / 15
|
 char* -> char (newbie question)
Quote: >At the moment I have: >char temparray[100][20]; /* D2 array 100x20 */
Try using manifest constants instead of literals for 20 and 100 Quote: >... >for (i=0; i<PQntuples(res); i++) > { > strcpy(temp_array[i][20], PQgetvalue(res, i, 0));
use strncpy, it is safer and will prevent over-running the end of a subarray. Now use temp_array[i]. What you did was to pass the value stored one beyond the end of the i'th sub array of temp_array. Quote: > printf("%s",temp_array[i][20]);
You were lucky that you got a segment fault when you tried brute force - by casting - because this told you there was something wrong with your code :) Quote: > }
Francis Glassborow Association of C & C++ Users 64 Southfield Rd Oxford OX4 1PA +44(0)1865 246490 All opinions are mine and do not represent those of any organisation --
|
Sun, 04 May 2003 03:00:00 GMT |
|
 |
Barry Schwar #15 / 15
|
 char* -> char (newbie question)
Quote:
>> OK. I have a 'simple' C problem which I cannot figure out. >> I have a function which returns a pointer (char *) which I call repeatedly >> from i to i_total. I want to fill an array of strings with the >> output but I am getting lost on how to do it. Can anyone send me some >> help? >> How should I define the array of strings (i.e. it is a multidimensional >> array) and should I use pointers or not. Then, how do I get each element >> of this array to contain the value returned by my function (I think I need >> strcpy here because I am reseting the function and will loose the data if >> I don't make a copy). >> At the moment I have: >> char temparray[100][20]; /* D2 array 100x20 */ snip >Randall... >How about: >for (i=0; i<PQntuples(res); i++) > { > strcpy(temp_array[i][0], PQgetvalue(res, i, 0)); > printf("%s",temp_array[i][0]); > } >Can you see why? >-- >Morris Dovey >West Des Moines, Iowa USA
Surely your compiler complains that temp_array[i][0] is not a pointer and the first parameter of strcpy must be a pointer or compatible type. Did you mean &temp_array[i][0]? The printf is equally bad but the compiler cannot catch that unless it evaluates the format string first which many do not. <<Remove the del for email>> --
|
Tue, 06 May 2003 03:00:00 GMT |
|
|
|