| | |
Array of pointers to char...messed up!
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
I want to finaly learn what's up with pointers and arrays, so i made up this program:
I had no errors nor warnings at all. But program crashes after entering the cantidad_frases value and hitting enter. The problem is in the 11th line.
I really didn't know how to "refer" each letter of the words/string stored in frases[i].
I hope i made my question clear enough so you can help me. Any advise or (please) any specific place where they explain the whole thing about pointers and arrays is more than welcome!
#include <stdio.h>
int main()
{
int cantidad_frases = 0;
printf("Cuantas frases quieres escribir?: ");
scanf("%d", &cantidad_frases);
char *frases[cantidad_frases];
for(unsigned i = 0; i < cantidad_frases; i++)
{
for(; *frases[i]; frases[i]++)
{
*frases[i] = getchar();
}
}
printf("\nReproduciendo las frases:\n\n");
for(unsigned i = 0; i < cantidad_frases; i++)
{
printf("Frase %d:", i + 1);
for(; *frases[i]; frases[i]++)
{
printf("%c", *frases[i]);
}
printf("\n");
}
return 0;
}I had no errors nor warnings at all. But program crashes after entering the cantidad_frases value and hitting enter. The problem is in the 11th line.
I really didn't know how to "refer" each letter of the words/string stored in frases[i].
I hope i made my question clear enough so you can help me. Any advise or (please) any specific place where they explain the whole thing about pointers and arrays is more than welcome!
0
#2 Oct 26th, 2009
This line should probably be:
char *frases[cantidad_frases];
So that you have allocated the memory...
char *frases[cantidad_frases];
C Syntax (Toggle Plain Text)
char *frases = (char*)malloc(cantidad_frases);
So that you have allocated the memory...
Last edited by gerard4143; Oct 26th, 2009 at 8:34 pm.
0
#3 Oct 27th, 2009
C Syntax (Toggle Plain Text)
for(unsigned i = 0; i < cantidad_frases; i++) { for(; *frases[i]; frases[i]++) { *frases[i] = getchar(); } }
Do
C Syntax (Toggle Plain Text)
#define MAX_STR 40 // whatever u want frases[i] = malloc(MAX_STR);
C Syntax (Toggle Plain Text)
for(; *frases[i]; frases[i]++)
C Syntax (Toggle Plain Text)
for(unsigned i = 0; i < cantidad_frases; i++) { frases[i] = malloc(MAX_STR); char *tmp = frases[i]; for(; *tmp; tmp++)//AGAIN HERE U SHOULD MODIFY YOUR LOGIC BECAUSE frases[i] initially contains nothing. I am leaving it to you. { *tmp = getchar(); } }
Last edited by dkalita; Oct 27th, 2009 at 3:15 am.
0
#5 Oct 27th, 2009
•
•
•
•
This line should probably be:
char *frases[cantidad_frases];
C Syntax (Toggle Plain Text)
char *frases = (char*)malloc(cantidad_frases);
So that you have allocated the memory...
•
•
•
•
C Syntax (Toggle Plain Text)
for(unsigned i = 0; i < cantidad_frases; i++) { frases[i] = malloc(MAX_STR); char *tmp = frases[i]; for(; *tmp; tmp++)//AGAIN HERE U SHOULD MODIFY YOUR LOGIC BECAUSE frases[i] initially contains nothing. I am leaving it to you. { *tmp = getchar(); } }
•
•
•
•
And in Cfor(unsigned i = 0; i < cantidad_frases; i++)is illegal.
imust be defined before it is used, not in theforstatement.
OK so i see my fault was all about memory allocation, so im going to learn about that cause i know nothing.
However, if you had this *frases[].. let's say it cointains:
*frases[0] -> "tutorials"
*frases[1] -> "dani web"
*frases[2] -> "it discussion"
How would you refer to the w in web at frases[1]?
Thank you
0
#6 Oct 27th, 2009
•
•
•
•
How would you refer to the w in web at frases[1]?
C Syntax (Toggle Plain Text)
frases[1][5];
C Syntax (Toggle Plain Text)
*(*(frases + 1)+ 5)
Last edited by Aia; Oct 27th, 2009 at 7:48 pm.
0
#7 Oct 28th, 2009
•
•
•
•
or if you want to complicate itC Syntax (Toggle Plain Text)
frases[1][5];C Syntax (Toggle Plain Text)
*(*(frases + 1)+ 5)
frases is pointing at 0, so +1 makes it look at the second phrase. And then the * dereference symbol, would return what? The first word in the first phrase? --> *( ? ) + 5 <---- ? should be a memory adress so it can be added with 5 and then dereference returning the "w" letter. But how come just doing *(frases + 1) returns not the value but a memory address?
Instead of mallocation before user's input, isn't the way to allocate at the same time user inputs so you reserve the exact memory needed? Like in run time.
Thank you for your time, i'm learning a lot. I'm gonna focus learning memory, malloc and pointers stuff more.
0
#8 Oct 29th, 2009
I made it! It works perfectly thanks to your advices and further studying hehe. Any comments / corrections?
I was turning mad when i saw phrase one was skipped. I realized there was a remanent \n in the buffer (by the line feed in the cantidad_frases selection). So i added the getchar() after the scanf to clean that up. Is there any elegant way to do it? EDIT: other than using another input function.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> int main() { int cantidad_frases = 0; printf("Cuantas frases quieres escribir?: "); scanf("%d", &cantidad_frases); getchar(); char *frases[cantidad_frases]; for(unsigned i = 0; i < cantidad_frases; i++) { frases[i] = (char *) malloc(sizeof(char[20])); char *tmp = frases[i]; printf("Escribe la frase %d: ", i+1); while((*tmp = getchar())) { if(*tmp == 10) { *tmp = '\0'; break; } tmp++; } } for (unsigned i = 0; i < cantidad_frases; i++) { puts(frases[i]); } return 0; }
I was turning mad when i saw phrase one was skipped. I realized there was a remanent \n in the buffer (by the line feed in the cantidad_frases selection). So i added the getchar() after the scanf to clean that up. Is there any elegant way to do it? EDIT: other than using another input function.
Last edited by neithan; Oct 29th, 2009 at 8:46 pm.
1
#9 Oct 30th, 2009
•
•
•
•
Any comments / corrections?
c Syntax (Toggle Plain Text)
int cantidad_frases = 0; printf("Cuantas frases quieres escribir?: "); scanf("%d", &cantidad_frases); getchar(); char *frases[cantidad_frases]; /* This is not allowed in C89 */
If you want dynamically allocated multidimensional arrays you may take a look at pointer to pointer. Example
C89 doesn't accept declaration of variable in the for loop construct.
for(unsigned i = 0; i < cantidad_frases; i++)When you ask for user input, the control of the program is handled to the user. They can enter an integer (in which case an enter key has been added to the stream), they can enter a float, a char or any combination and length of those. Moreover they might just press enter or be generous with blank sequence.
Can scanf() handle that kind of conditions? The answer is no. The elegant option then is to think how would you deal with the input and design your program to accommodate for those possibilities. There's not "silver-bullet function" that will do that for you.
0
#10 Oct 30th, 2009
•
•
•
•
In the C89 standard, an array must be set with a constant. Which would not allowed what you just did.
Furthermore, there can not be mixing of variable declarations and code. All variable must be declared first in the block. Making char *frases[cantid_frases]; incorrect as well.
•
•
•
•
If you want dynamically allocated multidimensional arrays you may take a look at pointer to pointer. Example
•
•
•
•
When you ask for user input, the control of the program is handled to the user. They can enter an integer (in which case an enter key has been added to the stream), they can enter a float, a char or any combination and length of those. Moreover they might just press enter or be generous with blank sequence.
Can scanf() handle that kind of conditions? The answer is no. The elegant option then is to think how would you deal with the input and design your program to accommodate for those possibilities. There's not "silver-bullet function" that will do that for you.
Thank you.
![]() |
Similar Threads
- Array of pointers to structure (C)
- Can you use an array of pointers for fgets? (C)
- reading names into char array of pointers (C++)
- double linked list array of pointers, on the right track? (C++)
- why i cann't execute my simple program(an array of pointers to object) (C++)
- Array of pointers (C++)
Other Threads in the C Forum
- Previous Thread: C program for 2D arrays
- Next Thread: File modification time as an int
Views: 534 | Replies: 11
| Thread Tools | Search this Thread |
Tag cloud for C
#include * .net append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi






