Sequence of Chars in C

Hello,

I want to allocate memory with pointer and get string from user.

I don't want to use an array. I have the below error.

Exception thrown at 0x7BDDE63C (ucrtbased.dll) in ConsoleApplication1c++.exe: 0xC0000005: Access violation writing location 0x00970000.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{

    char* sentence = (char*)malloc(100 * sizeof(char));
    do
    {
        scanf_s("%s[^\n]", (sentence));
        (*sentence)++;

    } while (*sentence != '-');

    printf_s("%s\n", sentence);
    printf_s("\n");


    return 0;
}



Thank you for your reply,

I changed to scanf and I want to print all elements in the pointer.
What's the problem?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
    char *sentence  = (char*)malloc(100 * sizeof(char));
    do
    {
        scanf("%s[^\n]", (sentence));
       // (*sentence)++;

    } while ((*sentence)++ != '-');

    char* P = sentence;

    while(*P)
    {
    printf("%s\n", (*P));
    P++;
    }
    printf("\n");


    return 0;
}



Last edited on
The format specifier in your printf statements is wrong. %s is used to output an entire C string.

Want to output a single element of a C string? %c.

http://www.cplusplus.com/reference/cstdio/printf/
Thank you for your answer,

I changed to
%c
but just printf singe
.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
    char *sentence  = (char*)malloc(100 * sizeof(char));
    do
    {
        scanf("%s[^\n]", (sentence));
       // (*sentence)++;

    } while ((*sentence)++ != '-');

    char* P = sentence;

    while(*P)
    {
    printf("%c\n", (*P));
    P++;
    }
    printf("\n");


    return 0;
}


Line 12:

} while ((*sentence)++ != '-');

(*sentence)++ increases the char not the pointer.

Alternativly to %c you may read the string like it is outside the loop. Find '-' and replace it with 0.
Thank you for your answer,

I changed code and I can read but I can't print array of chars.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
    char *sentence  = (char*)malloc(100 * sizeof(char));
    do
    {
       sentence++;
        scanf("%c", sentence);
    } while (*sentence != '-');
    char* P = sentence;

    while(*P )
    {
    printf("%c", *P);
    P++;
    }
    printf("\n");


    return 0;
}


Input is:
sdf
sgvsd
rges
-
and output is:(sometimes nothing)
-FILE_STRING=Default
Last edited on
1) By the time you get to line 13, you've changed the value of sentence so that it's pointing to the last character you entered. This means that you set P to point to the last character you entered, so that when you try and print the contents of the array, you're actually starting with the last character you entered.

2) Your loop at line 15 is based on whether or not the value of an element of the array is 0 (i.e. the null character '\0'). However, nowhere do you ever initialise the contents of the array to be 0, so you have no reason to believe any character in the array will have that value.

3) You increment sentence before you ever try to read any value into the array. This meand that you will never set any value in the first byte of the allocated memory.

4) Is there any reason you're using C? Are you aware that C++ has vastly better, safer string handling features? And dynamic memory allocation features?
Last edited on

With your help, I could fix my code.
Yes, I should C language.
Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
    char *sentence  = (char*)malloc(100 * sizeof(char));
    char* P = sentence;
    do
    {
        scanf("%c", sentence);
    } while (*sentence++ != '-');

    while(*P != '-')
    {
    printf("%c", *P);
    P++;
    }
    printf("\n");


    return 0;
}




You're welcome.

Why can't you use C++?
This question is my friend and he is a student and his teacher teach C language.
I want to release memory (20 and 21) and I read this article but it doesn't work for me.

https://www.geeksforgeeks.org/g-fact-30/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
    char *sentence  = (char*)malloc(100 * sizeof(char));
    char* P = sentence;
    do
    {
        scanf("%c", sentence);
    } while (*sentence++ != '-');

    while(*P != '-')
    {
    printf("%c", *P);
    P++;
    }
    printf("\n");

    delete sentence;
    delete p;

    return 0;
}


Last edited on
You need to use the function free(...) for each malloc(...). In this case a single free(...) for a single malloc(...).
new/delete is c++
malloc/free is C
you cannot delete where you used malloc. you cannot free where you used new. They will NOT work. malloc casting is considered odd in C, its required in C++ if you used malloc in c++ which you really should not in most cases (only reason is a very low level thing to allow a realloc).

a pointer IS an array, for all practical conversations (technically different of course):
you can use [] syntax on it
it is a solid block of memory
you can do the same core set of things to both, pointers have a few more operations. Stack vs heap is a thing but its irrelevant apart from managing the size/use of your stack, which isnt shown in the code proper but is at the high level comprehension of the program conceptual level.
That means you can get code working with arrays and change to pointers after testing, if that helps debug stuff. you can pass the name of an array into a function that wants a pointer.

there is NO reason to use a pointer here. 100 bytes is microscopic, and memory managment is slower, code bloated, and error prone. You do need to understand pointers and how to do this if you write C, but even in C, use an array for small blocks of memory, and let the stack handle the memory management stuff for you. Its faster and cleaner.

Make sure you are using a C compiler or telling the C++ compiler that you have C code. A C++ compiler will let you write a hybrid of the 2 languages that may not be correct in pure C.
Last edited on
Topic archived. No new replies allowed.