0

I wanted to allocate a string array dynamically , the program is supposed to ask the user for characters and collect them in the first string in the array till letter 'q' is entered then the program starts adding the characters to the second line and so on.

When I print the characters memory location it seems like each character takes two memory cells instead of one though I increment it only once.

Here is the source code to my program

#include <stdio.h>

void main()
{

    char** txt;
    char* baseAddress;

    txt = (char **)malloc(5*sizeof(char*));
    *txt = (char *)malloc(20*sizeof(char));

    baseAddress = *txt;


    while (*(*txt) != 'q')
    {
       (*txt)++;
       scanf("%c", (*txt));
       printf("%p\r\n", (*txt));
    } 

    *txt = '\0';

    printf("%p", baseAddress);

    free(baseAddress);
    free(txt);
}

the output is like this

>j
>0x9e12021
>0x9e12022
>s
>0x9e12023
>0x9e12024
>

I think there may be something wrong with the pointers. How do I go about accomplishing this? and sorry for the bad english

zentrunix
  • 2,098
  • 12
  • 20
  • The initial check against 'q' is against an indeterminate value (the value at `*(*txt)` has not been set yet). You may want to fix that as well as the other errors. – WhozCraig Sep 01 '13 at 03:29
  • The return type of `main()` is `int` — in standard C and in Microsoft C. Contrary to anything you see in text books, `void main()` is not an acceptable declaration for `main()` — though the Microsoft C compiler may not actually complain about it. And in MS code, you need `return 0;` or something at the end of `main()` because only C99 and later allows you to omit that return (a bug in the standard, IMNSHO, but it's far too late to worry about that), and MS only supports C89 which did not have that special dispensation for `main()`. – Jonathan Leffler Sep 01 '13 at 03:33
  • 1. Where on earth are you getting `void main()` from? 2. Don't cast the return from `malloc()`. 3. Don't use `sizeof(char)`, it's always 1. 4. Don't use so many parentheses, just learn your operator precedence, it doesn't make things clearer to pepper your code with them like this. 5. As WhozCraig said, you're accessing `**txt` before you've written to it, which is bad. 6. Don't use `\r\n` when printing to `stdout` in normal text mode in C, it's just `\n`. 7. Always check what `malloc()` is actually returning, you may not get your memory. – Crowman Sep 01 '13 at 03:35

2 Answers2

1

You are forgetting about the newline character.

For instance, you are probably imagining your input to be "js". However, since you are hitting the enter key, it is actually "j\ns\n".

So, you are entering two characters at a time, and it is reading two characters at a time. Your code is behaving just as it should.

corahm
  • 141
  • 1
  • 5
1

What exacty you code do :

+----------------------------------------------+
|     +--------------------------------------+ |  
|  txt|*txt  |*txt+1 |*txt+2 |*txt+3 |*txt+4 | |
|     +--------------------------------------+ |
|       ^        ^    no memory alloc   ^      |
|       |        |_______not used_______|      |
|     +----+                                   |
|*txt |____|    <------------------------------+---you never give a value here,                            
| +1  |____|    <--j                           |
| +2  |____|    <-- '\n'                       |
| +3  |____|    <--s                           |
| +4  |____|    <-- '\n'                       |
|  .    .                                      |
|  .    .                                      |
|  .   ____                                    |
| +19 |____|                                   |
+----------------------------------------------+

So you need :

  1. re-write you while loop and deal with the '\n'
  2. when user type q , alloc new string memory and collect user input .

Suggust:

In you fisrt string

Use txt[0] instead of *txt , use txt[0][i] and i++ instead of **txt and (*txt)++ .

Hope can help. :-)

Lidong Guo
  • 2,817
  • 2
  • 19
  • 31