| | |
Array of pointers to char...messed up!
![]() |
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 28 Days Ago
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; 28 Days Ago at 8:34 pm.
0
#3 28 Days Ago
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; 28 Days Ago at 3:15 am.
0
#5 27 Days Ago
•
•
•
•
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 27 Days Ago
•
•
•
•
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; 27 Days Ago at 7:48 pm.
0
#7 26 Days Ago
•
•
•
•
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 25 Days Ago
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; 25 Days Ago at 8:46 pm.
1
#9 24 Days Ago
•
•
•
•
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 24 Days Ago
•
•
•
•
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
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory dynamic feet fflush fgets file fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking highest homework i/o inches include incrementoperators input interest kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix meter microsoft mqqueue mysql number odf open openwebfoundation owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling segmentationfault send sequential shape socket socketprograming socketprogramming stack standard string systemcall turboc unix user voidmain() wab win32api windows.h






