0

I'm having trouble when I try to store the stdin in a program inside char array variable.

It throws a segfault when it goes by these lines:

procNames[processNumber] = argv[1];

and

strcpy(procNames[processNumber], proc[0]);

How can I store the chars in the array procNames?

The usage is:

(stdin) <CHAR>: <NUMBER>

I want to store every <CHAR> and every <NUMBER> introduced by order. The <NUMBER> stores without erros, the <CHAR> storage throws the segmentation fault.

char line[80],proc[80];

// Storing
char procNames[80];
int procPorts[80];

// To iterate
int processNumber = 0;
int actualProcessNumber = 0;

[...]

for(;fgets(line,80,stdin);) {

    sscanf(line,"%[^:]: %d",proc,&port);

    [...]

    if(strcmp(proc,argv[1]) == 0) {
        if (repeatedProc == false) {
            procNames[processNumber] = argv[1];
            procPorts[processNumber] = puerto_udp;
            actualProcessNumber = processNumber;
            processNumber++;
        }

    } else {
        if (repeatedProc == false) {
            strcpy(procNames[processNumber], proc[0]);
            procPorts[processNumber] = port;
            processNumber++;
        }
    }
}

Can someone please help me?

1
  • 1
    Understand that the Command Line for a program is not stdin. It is an argument list that is passed by the shell to your program on startup. stdin is a separate and distinct standard string that allows you to read input from the terminal. Commented Jun 4, 2020 at 16:02

3 Answers 3

3

Regarding the issues you get:

1.

You need

char procNames[N][80];

instead of

char procNames[80];

where N gives the amount of strings to hold in procNames. [80] - 1 just specifies the maximum amount of characters possible in each string.

2.

You cannot assign arrays with strings by the = operator in C. Use strcpy() instead.

Replace

procNames[processNumber] = argv[1];

with

strcpy( procNames[processNumber], argv[1] );

3.

strcpy(procNames[processNumber], proc[0]);
  1. The second argument of needs to be a pointer to char, proc[0] is of type char. Use proc only.
  2. proc has no string in it to copy. Use at least char proc[80] = ""; to not get a runtime error.
Sign up to request clarification or add additional context in comments.

Comments

3

Your procNames is an array of characters, not an array of pointers. Arrays cannot be assigned, just copied, and procNames[processNumber] = argv[1] should actually issue a warning / an error.

Further, as you intend to have an array of - let's say - 10 such names, you probably mean

char procNames[10][80];

Then you can write

strcpy(procNames[processNumber],argv[1]);

to copy the contents of the string argv[1] points to. Furhter, in order to avoid that you exceed the length of a procNames-entry, I suggest to use

strncpy(procNames[processNumber],argv[1],80); 

1 Comment

That's exactly what I needed to fix the first part, thx a million!
2

You defined char procNames[80]; which means it's a string that can hold 80 characters (also counting the 0-terminator at the end).

Later one you're doing this procNames[processNumber] = argv[1]; where procNames[processNumber] points to a character and argv[1] is a string holding the first command line parameter. So in fact you're trying to assign a pointer to a char.

Your compiler must have at least warned you about this.

Make sure to really look at compiler output as it often tries to tell you what you are doing wrong.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.